240 likes | 364 Views
Today’s Agenda. Design Patterns - A few examples . Observer Pattern – Classification & Applicability. A behavioral (object) pattern: Concerns objects and their behavior. Applicability Vary and reuse 2 different abstractions independently.
E N D
Today’s Agenda Design Patterns - A few examples BITS, Pilani. Sundar B.
Observer Pattern – Classification & Applicability • A behavioral (object) pattern: • Concerns objects and their behavior. • Applicability • Vary and reuse 2 different abstractions independently. • Change to one object requires change in (one or more) other objects – whose identity is not necessarily known BITS, Pilani. Sundar B.
observers Observer Update() Subject For all x in observers{ x.Update(); } attach (Observer) detach (Observer) Notify () Concrete Observer Concrete Subject subject Update() GetState() SetState() observerState subjectState observerState= subject.getState(); Observer Pattern – Structure BITS, Pilani. Sundar B.
Observer Pattern - Participants • Subject • Has a list of observers; • interfaces for attaching/detaching an observer • Observer • An updating interface for objects that gets notified of changes in a subject. • ConcreteSubject • Stores state of interest to observers • Sends notification when state changes. • ConcreteObserver • Implements updating interface. BITS, Pilani. Sundar B.
:ConcreteSubject :ConcreteObserver-1 :ConcreteObserver-2 SetState() Notify() Update() GetState() Update() GetState() Observer Pattern - Collaborations
public void addObserver(Observer o) {} Java terminology for Subject. public void deleteObserver (Observer o) {} public void notifyObservers(Object arg) {} Observer Pattern - Implementation interface Observer { void update (Observable sub, Object arg) } class Observable { … } public boolean hasChanged() {}
Observer Pattern - Implementation public PiChartView implements Observer { A Concrete Observer. void update(Observable sub, Object arg) { // repaint the pi-chart } } class StatsTable extends Observable{ public boolean hasChanged() { // override to decide when it is considered changed } }
Observer Pattern - Consequences • Abstract coupling between subject and observer. (subject need not know concrete observers) • Support for broadcast communication (all observers are notified) • Unexpected updates (observers need not know when updates occur) BITS, Pilani. Sundar B.
Name Classification Motivation Problem Scenario Applicability Situations Structure Graphical representation Often just the classes in the pattern Participants Collaborations How participants collaborate to carry out responsibilities? Implementation Consequences Related Patterns Patterns Documentation BITS, Pilani. Sundar B.
Patterns Classification • Basic Classification • by Purpose • Creational vs. Structural vs. Behavioral • by Scope • Object vs. Class • Domain Level Classification: • Patterns in Telecom Software • Patterns in Distributed Computing BITS, Pilani. Sundar B.
Second Example: Composite Pattern • Classification: • Structural and Object • Applicability • To represent part-whole hierarchies of objects • For clients to be able to ignore the difference between compositions of objects and individual objects. BITS, Pilani. Sundar B.
is-a is-a is-a is-a Semantic Element Text Formatter Table is-a Form Composite Pattern – Problem Scenario Element 1..n composed of Anchor Layout Container
Composite Pattern – Structure Component Add (Component) Remove (Component) GetChild(int) Operation () Composite Leaf Add(Component) Remove(Component) GetChild(int) Operation() Operation() Client children For all g in children{ g.Operation(); }
Composite Pattern – Participants • Component • Declares interface for objects and for accessing children • Implements default behavior • Leaf • No children; defines behavior for primitive objects • Composite • Defines behavior for components with children • Stores children and implements children-related operations • Client • Manipulates objects in the composition thru’ Component interface. BITS, Pilani. Sundar B.
Composite Pattern - Consequences • Defines Class hierarchies for recursive composition. • Makes clients simple (can treat composite structures and individual objects uniformly) • Makes it easy to add new components (no code needed for components or for clients) • Can make your design overly general – Harder to restrict the components of a composite. BITS, Pilani. Sundar B.
Composite Pattern – Related Patterns • Iterator Pattern – (Object Behavioral) • Enumeration of Children. • Visitor Pattern – (Object Behavioral) • Operation to be applied on each object (component) of composition. • Adding method in each object causes code cluttering particularly if operations are related. • Different operations to be applied on same hierarchy. BITS, Pilani. Sundar B.
Example 3: Abstract Factory - The Problem 1. Consider a user interface toolkit to support multiple look-and-feel standards. 2. For portability an application must not hard code its widgets for one look and feel. How to design the application so that incorporating new look and feel requirements will be easy? BITS, Pilani. Sundar B.
Abstract Factory Pattern 1. Define an abstract WidgetFactory class. 2. This class declares an interface to create different kinds of widgets. 3. There is one abstract class for each kind of widget and concrete subclasses implement widgets for different standards. 4. WidgetFactory offers an operation to return a new widget object for each abstract widget class. Clients call these operations to obtain instances of widgets without being aware of the concrete classes they use. BITS, Pilani. Sundar B.
Client WidgetFactory Window CreateScrollbar() WWindow CreateWindow() MacWindow ScrollBar WWidgetFactory MacScrollBar WScrollBar MacWidgetFactory One for each standard. Abstract Factory: Instance Structure
AbstractFactory ConcreteFactory2 Client ProductA1 ProductA2 AbstractProductA AbstractProductB CreateProductA() CreateProductA() CreateProductB() CreateProductB() ConcreteFactory1 ProductB2 ProductB1 Abstract Factory: Structure
Abstract Factory Pattern: Participants and Communication • AbstractFactory: Declares the interface for operations to create abstract product objects • ConcreteFactory: Implements the operations to create concrete product objects. • AbstractProduct: Declares an interface for a type of product object. • ConcreteProduct: Defines a product object to be created by the corresponding factory. • Client: Uses only the interface declared by the abstractFactory and AbstractProduct classes. BITS, Pilani. Sundar B.
Abstract Factory Pattern – Classification & Consequences • Classification: • Object Creational • Consequences: • Isolates concrete classes • Makes exchanging product families easy. • Promotes consistency among products • Supporting new kinds of products is not easy! BITS, Pilani. Sundar B.
Abstract Factory – Related Patterns • Factory Method • Used for implementing abstract factory classes • Class Creational • Singleton • Concrete factories are often Singletons. • Used when exactly one instance of a class is allowed. • Object Creational BITS, Pilani. Sundar B.
Further Reading • Design Patterns – Elements of Reusable OO Software • By E. Gamma, R. Helm, R. Johnson, J. Vlissides • For those who can’t handle the “abstract”: • Advanced C++ Programming – Styles and Idioms: by James Coplien BITS, Pilani. Sundar B.