Design Patterns

Creational Patterns

Description

It provides an interface for creating groups of related or dependent objects without specifying their concrete class.

Recipe

  1. Declare interfaces for each distinct product group
  2. Declare the Abstract Factory – an interface with a list of creation methods for all products of a given group. These methods must return abstract product types represented by the interfaces extracted previously
  3. For each variant of a product types, we create a separate factory class based on the AbstractFactory interface. A factory is a class that returns products of a particular kind.

Description

It separates the construction of a complex object from its representation

Recipe

  1. Extract the object construction code to separate objects – builders

Description

It provides an interface for creating an object, but subclasses decide which class to instantiate.

Recipe

  1. Replace direct object construction class calls to a special factory method

Description

It specifies the kinds of objects to create using a prototypical instance and creates new objects by copying this prototype.

Recipe

  1. Declare a common interface for all objects that support cloning

Description

It ensures that a class has only one instance and provides a global point of access to it.

Recipe

  1. Make the default constructor private, to prevent other objects from creating other instances of the Singleton class
  2. Create a static creation method that acts as a constructor. This method calls the private constructor to create an object and saves it in a static field. All other calls to return the cached object

Structural Design Patterns

Description

It converts the interface of a class into another interface that the client expects.

Recipe

  1. Create an object that converts the interface of one object to the interface that another object expects

Description

It decouples an abstraction from its implementation so that the two can vary independently.

Recipe

  1. Split implementation hierarchy and abstraction hierarchy

Description

It composes objects into tree structures to represent part-whole hierarchies.

Recipe

  1. Create common interface for all objects in a tree
  2. Implement interfaces for each object in such a way that it returns total of all inner objects and itself

Description

It attaches additional responsibilities to an object dynamically and provides a flexible alternative to sub-classing for extending. functionality

Recipe

  1. Create a helper class that adds new functionality
  2. Attach this helper to the object that functionality You want to modify

Description

It provides a unified interface to a set of interfaces in a subsystem.

Recipe

  1. Create a class with only interfaces that You need
  2. Make it call interfaces of a complex class or library that you want to use

Description

Is uses sharing to support large numbers of fine-grained objects efficiently.

Recipe

  1. Extract repeating, heavy state from an object and store it in another object
  2. Reference new object in, now lightweight object

Description

It provides a surrogate or placeholder for another object to control access to it.

Recipe

  1. Create a new proxy class with the same interface as an original object
  2. Make proxy object trigger delegation of all the work to original object

Behavioral Patterns

Description

It chains the receiving objects and passes the request along the chain until an object handles it.

Recipe

  1. Create handlers that replace particular behaviour
  2. Link these handlers into a chain by adding a field that stores a reference to the next handler in the chain
  3. Make handlers in addition to processing a request pass the request further along the chain

Description

It encapsulates a request as an object letting you parameterize clients with different requests, queue or log requests, and support undoable operations

Recipe

  1. Extract all of the request details (called object, method name, list of arguments) into a separate command class with a single method that triggers this request

Description

It defines a representation of a given language's grammar along with an interpreter that uses representation to interpret sentences in the language.

Recipe

  1. Create a representation of a given language grammar using a combination of classes and methods
  2. Create an interpreter that can interpret sentences in this language

Description

It provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Recipe

  1. Rxtract the traversal behavior of a collection into a separate object – iterator

Description

It defines an object that encapsulates how a set of objects interact.

Recipe

  1. Replace direct communication between the components with calling special object – mediator
  2. Make mediator redirect the calls to appropriate components

Description

It captures and externalizes an object's internal state so that the object can be restored to this state later.

Recipe

  1. Make the object create snapshots of its own state

Description

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

Recipe

  1. Add an array field to the publisher object for storing a list of references to subscriber objects
  2. Add public methods for adding and removing subscribers
  3. Add subscription mechanism that triggers appropriate change in subscribers

Description

It allows an object to alter its behavior when its internal state changes.

Recipe

  1. Create classes for all possible states of an object and extract all state-specific behaviors into these classes
  2. Add mechanism that switches main object state

Description

It defines a family of algorithms, encapsulates each one, and makes them interchangeable.

Recipe

  1. Extract all algorithms that do something specific in a lot of different ways into separate classes – strategies
  2. Add a field for storing a reference to the strategy to the original class – context
  3. Make context delegate the work to a linked strategy object

Description

It defines the skeleton of an algorithm in an operation, deferring some steps to subclasses.

Recipe

  1. Break down an algorithm into a series of steps and turn them into methods
  2. Put a series of calls to these methods inside a single template method

Description

It represents an operation to be performed on the elements of an object structure.

Recipe

  1. Instead of adding behavior to the original class place it into a new class – visitor
  2. Create a method in the visitor that gets an original object as an argument and executes new behavior


Sources:
Design Patterns: Elements of Reusable Object-Oriented Software (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides)
Refactorings to Patterns (Joshua Kerievsky)
Design Patterns (Refactoring Guru)
Design Patterns Quick Reference (Jason McDonald)