Design Patterns

From Colettapedia
Jump to navigation Jump to search

Design Patterns

  • generaly reusable solution to a commonly encountered situation
  • common approach idiom terminology vocab
  • mental framework
  • recommended approach

Creational Patterns

  • Abstract the instantiation process
  • Class creational pattern - uses inheritance to vary class that's instantiated
  • Object creational pattern - delegate instantiation to another object
  • Creational patterns encapsulate knowledge about which concrete classes the sytem uses, and hide how instances of these classes are created and put together.
  • All the system knows about the objects is their interfaces as defined by abstract classes.
  • Creational patterns provide different ways to remove explicit references to concrete classes from code that needs to instantiate them.

Abstract Factory

  • Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Pass the whole concrete (subclassed) factory to the object creation method as a parameter, but pass it as a pointer to the abstract factory class.
  • method calls virtual functions
  • so two constructions:
    • abstract WidgetFactory with virtual functions to create x variety of y widgets
    • for each x variety, create a per-variety class that implements the virtual functions it inherits
    • for each y widget type, there's a base class, and each variety of widget is a subclass
  • Use when:
    1. a system should be independent of how its products are created, composed, and represented
    2. a system should be configured with one of multiple families of products
    3. a family of related product objects is designed to be used together, and you need to enforce this constraint
    4. you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations


  • separate the construction of a complex object from its representation so that the same construction process can create different representations
  • add a new output type without modifying the
  • use when construction process must allow for different representations for the object that's constructed.

Factory Method

  • build me an object that fits this interface
  • Define an interface for creating an object, but let subclasses decide which class to instantiate.
  • Defers instantiation to subclasses.
  • E.g., application subclasses redefine an abstract CreateDocument operation on Application, to return the appropriate Document subclass.
  • CreateDocument is a "factory method" because it's responsible for manufacturing an object.
  • eliminate the need to bind application-specific classes into your code.
  • creating objects inside a class with a factory method is always more flexible than creating an object directly
  • use when
    • a class can't anticipate the class of objects it must create
    • classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegates
  • Participants: Products are the objects being created, and Creators are the factories
    • Product - defines the interface of objects the factory method creates (Document)
    • ConcreteProduct - implements the Product interface (MyDocument)
    • Creator - declares the factory method, which returns an object of type Product. Creator may also define default implementation of factory method that returns a default ConcreteProduct object. May call the factory method that returns default ConcreteProduct object(Application)
    • ConcreteCreator - overrides teh factory method to return an instance of ConcreteProduct


  • specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype
  • use to avoid building a class hierarchy of factories that parallels the class hierarchy of products
  • it may be more convenient to install a corresponding number of prototypes and clone rather than instantiating the class manually each time with appropriate state.
  • participants:
    • Prototype - declares an interface for cloning itself
    • ConcretePrototype - implements an operation for cloning itself
    • Client - creates a new object by asking a prototype to clone itself


  • ensure a class has only one instance, and provide a global access point to it
  • make the class itself responsible for keeping track of its sole instance
  • reduced name space pollution by using less global variables

Structural Patterns

  • Concerned with how classes and objects are composed to form larger structures.

Adapter (Wrapper)

  • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
  • The adapted class is inherited privately.


  • Decouple an abstraction from its implementation so that the two can vary independently


  • Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
  • Describes how to build a class hierarchy made up of classes for two kinds of objects: primitive and composite.


  • Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extended functionality


  • Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.


  • Use sharing to support large numbers of fine-grained objects efficiently


  • Provide a surrogate or placeholder for another object to control access to it.
  • A convenient surrogate or place holder for another object.
    • Can be local representative for an object in remote address space.
    • Can be large object that should be loaded on demand
    • Might protect access to a sensitive object
  • Provides a level of indirection to specific properties of objects, in order to restrict, enhance or alter these properties.

Behavioral Patterns

  • Encapsulating variation. Encapsulate teh part of the program that changes frequently
  • Usually define an abstract class that describes the encapsulating object, and teh pattern derives its name from the object
    • Strategy object encapsulates an algorithm
    • State object encapsulates a state-dependent behavior
    • a mediator object encapsulates teh protocol between objects
    • an iterator object encapsulates the way you access and traverse the components of an aggregate object

Chain of Responsibility

  • Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle teh request. Chain the receiving objects and pass the request along the chain until an object handles it.


  • Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.


  • Given a language, define a representation for its grammar aling with an interpreter that uses the representation to interpret sentences in the language.


  • Provide a way to accessthe elements of an aggregate object sequentially without exposing its underlying representation.


  • Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.


  • Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to that state later.


  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.


  • Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.


  • Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets algorithm vary independently from clients that use it.

Template Method

  • Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template MEthod lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure


Represent an operation to be performed on the elements of an objects structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Notes from DrupalCon2009

  • Observer
    • event-driven programming
    • notify, broadcast out in case you care to do something, gets notified and does something in response
    • active observer - explicitly add observer, list all the observers, somewhere in the class
  • Visitor
    • object, and another object that encapsulates method, pass state
  • factory
  • indirection
    • powerful, prevent spaghetti code, sometimes expensive, sometimes harder to read/debug, more clock cycles, has conceptual overhead
  • oo syntax is indirection