Design Patterns Cheat Sheet

Creational Patterns Abstract Factory Provides an interface for creating families of related or dependent objects without specifying their concrete classes AbstractFactory +CreateProductA() +CreateProductB() Client ProductA AbstractProduct ProductB ConcreteFactory +CreateProductA() +CreateProductB()
creates

Structural Patterns (cont’d) Bridge Decouples an abstraction from its implementation so that the two can vary independently Abstraction Client +Operation() ConcreteImplementorA Implementor +OperationImpl() +OperationImpl() ConcreteImplementorB +OperationImpl()

Builder Separates the construction of a complex object from its representation so that the same construction process can create different representations. Director +Construct() Builder +BuildPart() Client Product
builds

Composite Composes objects into tree structures to represent part-whole hierarchies Composite Component +Operation() +Add(component) +Remove(component) +GetChild(index) +Operation() +Add(component) +Remove(component) +GetChild(index) Leaf +Operation()

ConcreteBuilder +BuildPart()

Factory Method Decorator Defines an interface for creating an object but let subclasses decide which class to instantiate Product Creator +FactoryMethod() Component +Operation() ConcreteProduct
creates

Attaches additional responsibilities to an object dynamically ConcreteComponent +Operation() Decorator +Operation() ConcreteDecorator +Operation() +AddedBehavior()

ConcreteCreator +FactoryMethod()

Prototype Specifies the kinds of objects to create using a prototypical instance and create new objects by copying this prototype ConcretePrototype1 Prototype Client +Clone() +Clone() ConcretePrototype2 +Clone() Subsystem

Facade Provides a unified interface to a set of interfaces in a subsystem Facade

Flyweight Singleton Ensure a class only has one instance and provide a global point of access to it Singleton -instance -Singleton() +GetInstance() Flyweight +Operation(state) Uses sharing to support large numbers of fine-grained objects efficiently FlyweightFactory +GetFlyweight(key) Client UnsharedFlyweight +Operation(state) Flyweight +Operation(state) Structural Patterns Adapter Converts the interface of a class into another interface clients expect Target Client +Request() Subject Client Adapter +Request() Adaptee +SpecificRequest() +Request() RealSubject +Request()

Proxy Provides a surrogate or placeholder for another object to control access to it Proxy +Request()

defines a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language Aggregate +CreateIterator() Client Iterator +First() +Next() +CurrentItem() ConcreteIterator +Next() ConcreteAggregate +CreateIterator() Strategy +AlgorithmInterface() +AlgorithmInterface() StrategyB +AlgorithmInterface() TemplateMethod Defines the skeleton of an algorithm in an operation. queue or log requests.Design Patterns Cheat Sheet Behavioral Patterns Chain of Responsibility Avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request ConcreteHandler1 Handler Client +HandleRequest() +HandleRequest() ConcreteHandler2 +HandleRequest() ConcreteSubject -subjectState Command Encapsulates a request as an object. and support undoable operations Client executes Behavioral Patterns (cont’d) Observer Defines a one-to-many dependency between objects so that when one object changes state all its dependents are notified and updated automatically Subject +Attach(observer) +Detach(observer) +Notify() Observer +Update() ConcreteObserver -observerState +Update() +HandleRequest() State Allows an object to alter its behavior when its internal state changes Context Invoker Receiver +Action() ConcreteCommand +Execute() Command +Execute() +Request() ConcreteStateA State +Handle() ConcreteStateB +Handle() Interpreter Given a language. defines a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language Client Context TerminalExpression AbstractExpression +Interpret(context) +Interpret(context) +Handle() Strategy Defines a family of algorithms. capture and externalize an object's internal state so that the object can be restored to this state later Originator -state +SetMemento(memento) +CreateMemento() Memento -state +GetState() +SetState() Caretaker Element +Accept(visitor) Visitor +VisitElementA(element) +VisitElementB(element) ConcreteVisitor +VisitElementA(element) +VisitElementB(element) ConcreteElementA +Accept(visitor) ConcreteElementB +Accept(visitor) TemplateMethod Represents an operation to be performed on the elements of an object structure . thereby letting you parameterize clients with different requests. and make them interchangeable Context NonterminalExpression +Interpret(context) +ContextInterface() StrategyA Iterator Given a language. encapsulate each one. deferring some steps to subclasses AbstractClass +TemplateMethod() +PrimitiveOperation1() +PrimitiveOperation2() ConcreteClass +PrimitiveOperation1() +PrimitiveOperation2() Mediator Defines an object that encapsulates how a set of objects interact Mediator ConcreteMediator ConcreteColleague1 Colleague ConcreteColleague2 Client Memento Without violating encapsulation.