Professional Documents
Culture Documents
Creational Patterns Abstract Factory Builder Factory Method Prototype Singleton Behavioral Patterns Chain of Responsibility Command Interpreter Iterator Mediator
Structural Patterns Adapter Bridge Composite (Sammys Slides) Decorator (Sammys Slides) Facade Flyweight Proxy
MyDocument
MyApplication
return new MyDocument CreateDocument()
Applicability
Use the Factory Method pattern when
a class cant 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.
ConcreteProduct
ConcreteCreator
return new ConcreteProduct FactoryMethod()
Participants
Product Defines the interface of objects the factory method creates ConcreteProduct 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. ConcreteCreator Overrides factory method to return instance of ConcreteProduct
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 carFactory class to produce car objects Can change cars by changing carFactory
Factory Example
class 350Z implements Car; class Ram implements Car; class Accord implements Car; Car fast = new 350Z(); // fast car // truck // family car // returns fast car
public class carFactory { public static Car create(String type) { if (type.equals("fast")) return new 350Z(); if (type.equals("truck")) return new Ram(); else if (type.equals(family) return new Accord(); } } Car fast = carFactory.create("fast"); // returns fast car
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
static Instance()
SingletonOperation() GetSingletonData()
Singleton Example
public class Employee { public static final int ID = 1234; // ID is a singleton } public final class MySingleton { // declare the unique instance of the class private static MySingleton uniq = new MySingleton(); // private constructor only accessed from this class private MySingleton() { } // return reference to unique instance of class public static MySingleton getInstance() { return uniq; } }
Adapter Pattern
Definition
Convert existing interfaces to new interface
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 PriorityQueue { // Priority Queue void add(Object o); int size(); Object removeSmallest(); }
Adapter Example
public class PriorityQueueAdapter implements PriorityQueue { Set s; PriorityQueueAdapter(Set s) { this.s = s; } public void add(Object o) { s.add(o); } int size() { return s.size(); } public Integer removeSmallest() { Integer smallest = Integer.MAX_VALUE; Iterator it = s.iterator(); while ( it.hasNext() ) { Integer i = it.next(); if (i.compareTo(smallest) < 0) smallest = i; } s.remove(smallest); return smallest; } }
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
observers
Observer
Update()
Concrete Subject
GetState() SetState() subjectState
subject
Update()
observerState
observerState= subject.getState();
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.
void update (Observable sub, Object arg) Java terminology for Subject.
}
class Observable {
public void addObserver(Observer o) {} public void deleteObserver (Observer o) {} public void notifyObservers(Object arg) {}
}
}