List of Design Patterns Creational Patterns Abstract Factory
List of Design Patterns • Creational Patterns – Abstract Factory – Builder – Factory Method – Prototype – Singleton • Behavioral Patterns – Chain of Responsibility – Command – Interpreter – Iterator – Mediator • Structural Patterns – Adapter – Bridge – Composite (Sammy’s Slides) – Decorator (Sammy’s Slides) – Facade – Flyweight – Proxy – – – Memento Observer State Strategy Template Method Visitor
FACTORY METHOD (Class Creational) • Intent: – Define an interface for creating an object, but let subclasses decide which class to instantiate. – Factory Method lets a class defer instantiation to subclasses.
FACTORY METHOD (Class Creational) • Motivation: – Framework use abstract classes to define and maintain relationships between objects – Framework has to create objects as well must instantiate classes but only knows about abstract classes - which it cannot instantiate – Factory method encapsulates knowledge of which subclass to create - moves this knowledge out of the framework
FACTORY METHOD Motivation Document Open() Close() Save() Revert() My. Document docs Application Create. Document() New. Document() Open. Document() Document* doc=Create. Document(); docs. Add(doc); doc->Open(); My. Application Create. Document() return new My. Document
Applicability • Use the Factory Method pattern when – a class can´t anticipate the class of objects it must create. – a class wants its subclasses to specify the objects it creates. – classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
FACTORY METHOD Structure Creator Product Factory. Method() An. Operation() Concrete. Product . . . product = Factory. Method(). . . Concrete. Creator Factory. Method() return new Concrete. Product
Participants • Product – Defines the interface of objects the factory method creates • Concrete. Product – Implements the product interface • Creator – Declares the factory method which returns object of type product – May contain a default implementation of the factory method – Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate Concrete Product. • Concrete. Creator – Overrides factory method to return instance of Concrete. Product
Factory Pattern • Example – Car Factory produces different Car objects – Original • Different classes implement Car interface • Directly instantiate car objects • Need to modify client to change cars – Using pattern • Use car. Factory class to produce car objects • Can change cars by changing car. Factory
Factory Example class 350 Z implements Car; class Ram implements Car; class Accord implements Car; Car fast = new 350 Z(); // fast car // truck // family car // returns fast car public class car. Factory { public static Car create(String type) { if (type. equals("fast")) return new 350 Z(); if (type. equals("truck")) return new Ram(); else if (type. equals(“family”) return new Accord(); } } Car fast = car. Factory. create("fast"); // returns fast car
SINGELTON (Object Creational) • Intent: – Ensure a class only has one instance, and provide a global point of access to it. • Motivation: – Some classes should have exactly one instance (one print spooler, one file system, one window manager) – A global variable makes an object accessible but doesn’t prohibit instantiation of multiple objects – Class should be responsible for keeping track of its sole interface
Applicability • Use the Singleton pattern when – there must be exactly one instance of a class, and it must be accessible to clients from a well -known access point. – when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.
SINGLETON Structure Singleton static Instance() Singleton. Operation() Get. Singleton. Data() Static uniquelnstance singleton. Data return uniquelnstance
Participants and Collaborations • Singleton: • Defines an instance operation that lets clients access its unique interface • Instance is a class operation (static in Java) • May be responsible for creating its own unique instance • Collaborations: • Clients access a Singleton instance solely through Singleton’s Instance operation.
Singleton Example public class Employee { public static final int ID = 1234; // ID is a singleton } public final class My. Singleton { // declare the unique instance of the class private static My. Singleton uniq = new My. Singleton(); // private constructor only accessed from this class private My. Singleton() { … } // return reference to unique instance of class public static My. Singleton get. Instance() { return uniq; } }
Adapter Pattern • Definition – Convert existing interfaces to new interface • Where to use & benefits – Help match an interface – Make unrelated classes work together – Increase transparency of classes
Adapter Pattern • Example – Adapter from integer Set to integer Priority Queue – Original • Integer set does not support Priority Queue – Using pattern • Adapter provides interface for using Set as Priority Queue • Add needed functionality in Adapter methods
Adapter Example public interface Priority. Queue { // Priority Queue void add(Object o); int size(); Object remove. Smallest(); }
Adapter Example public class Priority. Queue. Adapter implements Priority. Queue { Set s; Priority. Queue. Adapter(Set s) { this. s = s; } public void add(Object o) { s. add(o); } int size() { return s. size(); } public Integer remove. Smallest() { Integer smallest = Integer. MAX_VALUE; Iterator it = s. iterator(); while ( it. has. Next() ) { Integer i = it. next(); if (i. compare. To(smallest) < 0) smallest = i; } s. remove(smallest); return smallest; } }
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
Observer Pattern – Structure Subject observers Observer Update() attach (Observer) detach (Observer) Notify () For all x in observers{ x. Update(); } Concrete Observer Concrete Subject Get. State() Set. State() subject. State subject Update() observer. State= subject. get. State();
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. • Concrete. Subject – Stores state of interest to observers – Sends notification when state changes. • Concrete. Observer – Implements updating interface.
Observer Pattern - Collaborations : Concrete. Subject : Concrete. Observer-1 : Concrete. Observer-2 Set. State() Notify() Update() Get. State()
Observer Pattern Implementation interface Observer } { void update (Observable sub, Object arg) class Observable { Java terminology for Subject. public void add. Observer(Observer o) {} {} public void notify. Observers(Object arg) {} public void delete. Observer (Observer o) … } public boolean has. Changed() {}
Observer Pattern Implementation public Pi. Chart. View implements Observer { } A Concrete Observer. void update(Observable sub, Object arg) { // repaint the pi-chart } class Stats. Table extends Observable{ public boolean has. Changed() { // 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)
- Slides: 25