Presentation is loading. Please wait.

Presentation is loading. Please wait.

July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652.

Similar presentations


Presentation on theme: "July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652."— Presentation transcript:

1 July 28, 2015IAT 2651 Design Patterns

2 “Gang of Four” July 28, 2015IAT 2652

3 Design Advice  Program to an Interface, not an implementation  Composition over Inheritance –Build software by composing objects together –Inheritance is often difficult to manage Changes to parent class may mess up child classes July 28, 2015IAT 2653

4 Interface  If you write code to implement an interface –There is no possibility for parent changes to mess up child classes –Unfortunately, you can’t re-use code via inheritance July 28, 2015IAT 2654

5 Balance Inheritance  Inheritance is useful where: –There is a strong IS-A relationship –Child class uses all of parent data –Child class does a lot of the same stuff the parent does  Not so good if –Child class ignores a lot of parent data –Child class adds new behaviors and ignores old behaviors July 28, 2015IAT 2655

6 Design Advice  As with any design problem  It is Rarely the case that the first design is the best  As with any design task, be prepared to do it over July 28, 2015IAT 2656

7 Design Patterns  The Gang of Four offer the following design patterns, in 3 categories: –Creational Patterns: Patterns of objects that are about creating objects –Structural Patterns: Patterns of composing objects together –Behavioral: Patterns of communication between objects July 28, 2015IAT 2657

8 Creational: Factory  Factory method –a method in some class generates an instance of a class -- NOT a constructor! class ArmedRocket extends Rocket {... Missile fire() { Missile m = new Missile(xPos, yPos, rotation); return m; } July 28, 2015IAT 2658

9 Singleton pattern  Make only 1 instance of an object: Ever!  Useful when: there’s some single resource or state you want to manage public class SingletonDemo { private static SingletonDemo instance; private SingletonDemo() { } public static SingletonDemo getInstance() { if (instance == null ) { instance = new SingletonDemo(); } return instance; } July 28, 2015IAT 2659

10 Behavioral Patterns  Iterator  Provide a way to access the elements of an aggregate object (container, list) without exposing its underlying implemetation  In Java, it’s the Iterator interface Iterator itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); } July 28, 2015IAT 26510

11 Observer  A Subject –Maintains a list of observers, and notifies them whenever subject’s state changes  Used in almost every GUI toolkit/widget set –Slider “Observes” mouse events in its region  Also fundamental to CPU architecture as the Interrupt Service Routine July 28, 2015IAT 26511


Download ppt "July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652."

Similar presentations


Ads by Google