Creational Patterns
Description
It provides an interface for creating groups of related or dependent objects without specifying their concrete class.Recipe
- Declare interfaces for each distinct product group
- 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
- 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 representationRecipe
- 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
- 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
- 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
- Make the default constructor private, to prevent other objects from creating other instances of the Singleton class
- 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
- 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
- Split implementation hierarchy and abstraction hierarchy
Description
It composes objects into tree structures to represent part-whole hierarchies.Recipe
- Create common interface for all objects in a tree
- 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. functionalityRecipe
- Create a helper class that adds new functionality
- 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
- Create a class with only interfaces that You need
- 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
- Extract repeating, heavy state from an object and store it in another object
- Reference new object in, now lightweight object
Description
It provides a surrogate or placeholder for another object to control access to it.Recipe
- Create a new proxy class with the same interface as an original object
- 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
- Create handlers that replace particular behaviour
- Link these handlers into a chain by adding a field that stores a reference to the next handler in the chain
- 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 operationsRecipe
- 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
- Create a representation of a given language grammar using a combination of classes and methods
- 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
- 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
- Replace direct communication between the components with calling special object – mediator
- 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
- 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
- Add an array field to the publisher object for storing a list of references to subscriber objects
- Add public methods for adding and removing subscribers
- Add subscription mechanism that triggers appropriate change in subscribers
Description
It allows an object to alter its behavior when its internal state changes.Recipe
- Create classes for all possible states of an object and extract all state-specific behaviors into these classes
- Add mechanism that switches main object state
Description
It defines a family of algorithms, encapsulates each one, and makes them interchangeable.Recipe
- Extract all algorithms that do something specific in a lot of different ways into separate classes – strategies
- Add a field for storing a reference to the strategy to the original class – context
- 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
- Break down an algorithm into a series of steps and turn them into methods
- 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
- Instead of adding behavior to the original class place it into a new class – visitor
- 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)