Design Pattern Cheatsheet
Design Pattern Cheatsheet
Provides an interface for creating families of related or dependent objects without Decouples an abstraction from its implementation so that the two can vary
specifying their concrete classes independently
Abstraction
AbstractFactory ConcreteFactory Client
+Operation()
+CreateProductA() +CreateProductA()
+CreateProductB() +CreateProductB() ConcreteImplementorA
creates
Client +OperationImpl()
Implementor
ProductA
AbstractProduct +OperationImpl() ConcreteImplementorB
ProductB +OperationImpl()
Builder
Composite
Separates the construction of a complex object from its representation so that the
same construction process can create different representations. Composes objects into tree structures to represent part-whole hierarchies
Composite
Director Builder
+Operation()
+Construct() +BuildPart() +Add(component)
Component
+Remove(component)
+Operation() +GetChild(index)
Client +Add(component)
Product builds ConcreteBuilder +Remove(component)
+GetChild(index) Leaf
+BuildPart()
+Operation()
Factory Method
Decorator
Defines an interface for creating an object but let subclasses decide which class to
instantiate Attaches additional responsibilities to an object dynamically
Prototype Facade
Specifies the kinds of objects to create using a prototypical instance and create new Provides a unified interface to a set of interfaces in a subsystem
objects by copying this prototype
Facade
ConcretePrototype1
+Clone()
Prototype
Client
+Clone() Subsystem
ConcretePrototype2
+Clone()
Flyweight
Ensure a class only has one instance and provide a global point of access to it
FlyweightFactory
Client
+GetFlyweight(key)
Singleton
-instance UnsharedFlyweight
+Operation(state)
-Singleton() Flyweight
+GetInstance()
+Operation(state)
Flyweight
+Operation(state)
Structural Patterns
Adapter
Proxy
Converts the interface of a class into another interface clients expect
Provides a surrogate or placeholder for another object to control access to it
Target Proxy
Client
+Request() +Request()
Subject
Client
+Request()
Adapter Adaptee RealSubject
+Request() +SpecificRequest() +Request()
Design Patterns Cheat Sheet
Behavioral Patterns Behavioral Patterns (cont’d)
Chain of Responsibility Observer
Avoids coupling the sender of a request to its receiver by giving more than one object Defines a one-to-many dependency between objects so that when one object changes
a chance to handle the request state all its dependents are notified and updated automatically
ConcreteHandler1 Subject
Observer
+HandleRequest() +Attach(observer)
Handler +Detach(observer) +Update()
Client +Notify()
+HandleRequest()
ConcreteHandler2
+HandleRequest()
ConcreteSubject ConcreteObserver
-subjectState -observerState
Client Invoker Allows an object to alter its behavior when its internal state changes
executes Context
+Request()
Receiver ConcreteCommand Command
+Action() +Execute() +Execute()
ConcreteStateA
+Handle()
State
Interpreter
+Handle()
ConcreteStateB
Given a language, defines a representation for its grammar along with an interpreter
that uses the representation to interpret sentences in the language +Handle()
Client Context
Strategy
TerminalExpression Defines a family of algorithms, encapsulate each one, and make them interchangeable
+Interpret(context)
AbstractExpression
+Interpret(context) Context
NonterminalExpression
+ContextInterface()
+Interpret(context)
StrategyA
Iterator +AlgorithmInterface()
Strategy
Given a language, defines a representation for its grammar along with an interpreter +AlgorithmInterface()
StrategyB
that uses the representation to interpret sentences in the language
+AlgorithmInterface()
Aggregate ConcreteAggregate
+CreateIterator() +CreateIterator() TemplateMethod
Visitor ConcreteVisitor
ConcreteColleague1
Colleague +VisitElementA(element) +VisitElementA(element)
+VisitElementB(element) +VisitElementB(element)
ConcreteColleague2
Client ConcreteElementA
Memento +Accept(visitor)
Element
Without violating encapsulation, capture and externalize an object's internal state so +Accept(visitor)
that the object can be restored to this state later ConcreteElementB
+Accept(visitor)
Originator Memento
-state -state
Caretaker
+SetMemento(memento) +GetState()
+CreateMemento() +SetState()