Factory
Creational
Creation of objects that conform to the same interface without specifying concrete classes
Recipe
- Extract interface from objects,
- Create a 'Factory' class,
- Put in a switch case or other logic to create objects of different type (but same interface) into the factory class.
Abstract Factory
Creational
Same as Factory (Creation of objects that conform to the same interface without specifying concrete classes) but to 'families' of objects
Recipe
- Extract and declare an interface for each piece of a "family" of items (blueprint of a type of player: defender, attacker, quarter back),
- Extract and declare an interface for the "family" of items (blueprint of a team: Barcelona, Washington Capitals, New England Patriots),
- Create a factory that can create a family of items -- step towards creating a league.
Builder
Creational
Construction of complex objects by extracting the steps and centralizing the control of what happens at each step
Recipe
- Extract method steps into an interface,
- Customize what methods do and create various objects,
- Optional: have a Director class orchestrate the order of events.
Prototype
Creational
Copying of existing objects without a dependency on a concrete class that needs to be copied
Recipe
- Provide a method to clone the current object and control what the cloned version can and can't do,
- Profit
Singleton
Creational
Ensuring one same copy of a resource is available at any and all times.
Recipe
- Add a private static field to hold the instance of the object,
- Make the constructor of the class private,
- Create a public static method to(create if does not exist and then) return the instance of the object,
- Replace all calls in existing code use the method to get the instance of an object.
Adapter
Structural
A way to translate one format into another.
Recipe
- Create another entity,
- Provide references to the two (or more) entities that need to be interoperated,
- Provide functionality that makes the two (or more) entities work together.
Bridge
Structural
Decoupling Abstraction from Implementation
Recipe
- Identify an item that can be split into independent pieces,
- Extract interfaces,
- Develop classes implementing interfaces,
- Re-assemble pieces.
Composite
Structural
Working with tree-like components in a uniform way regardless of whether it's a leaf or a container
Recipe
- Extract an interface with a method,
- Create two classes that implement that interface, but implement the method differently,
- Ensure that the class that is the container with multiple elements calls that method on each element of the container.
Decorator
Structural
Enhancing functionality while ensuring single responsibility
Recipe
- Create an interface with a method that has the functionality that needs to be enhanced,
- Create a new class with extra functionality,
- Inject the source class into the new class,
- Provide more functionality as needed, preferrably without violating the Single Responsibility Principle,
- Repeat steps as needed until desired functionality is achieved.
Facade
Structural
Hiding complex procedures behind a wall and providing a push-button interface
Recipe
- Create a new class with one method that pushes all needed buttons in a required sequence,
- Expose one method from that class that does everything.
Flyweight
Structural
Creating a large number of similar objects while sharing some of their state to save memory
Recipe
- Figure out what varies among the many pieces and what can stay the same,
- Create a fast way that can check for and create if needed the classes with constant pieces of information,
- Provide a way to work with properties that vary.
Proxy
Structural
A placeholder for another object for a few reasons
Chain of Responsibility
Behavioral
Standardizing similar separate functionality
Recipe
- Set up a special interface for steps needing to be performed,
- Flesh out concrete steps,
- Connect steps in a chain,
- Work an item through the chain.
Command
Behavioral
Abstracting callable actions such that they can be assigned to multiple callers and support undoing
Interpreter
Behavioral
Setting up grammar, sentence structure, and mechanism(s) for translating
Recipe
- Declare a Context -- a reusable template class,
- Set context's input to what needs to be translated,
- Output does not need to be set, or start with some initial value,
- Declare an abstract class (usually called Expression) and declare one method -- Interpret, takes a Context as input and does the magic inside,
- Then create another class inheriting from Expression and flesh out implementation for the Interpret method,
- Then create as many other classes as needed to convert into other things,
- Need to identify if context has non-terminal expressions and handle those accordingly.
Iterator
Behavioral
Setting up a way to traverse a collection without exposing its inner structure
Recipe
- Step 1, think long and hard if a custom iterator is really needed,
- Declare an interface that outlines how the collection is traversed,
- Declare another class that implements one method, getIterator(). Use that class to ensure the collection is iterable,
- Alternatively, skip this class and implement checks in the constructor of the class that returns iterator,
- In the actual iterator class, implement the methods declared in the interface from step 2.
Mediator
Behavioral
Abstracting communication function to a dedicated entity
Recipe
- Identify the need,
- Declare a communication entity, teach it to communicate to all parties involved,
- Explain objects the new way of communicating to each other.
Memento
Behavioral
An ability to provide and consume internal state via a predefined channel
Recipe
- Identify all elements of the state that need to be serialized/deserialized for successful state transfer,
- Provide a way to save those elements,
- Provide a way to process an entity containing those elements such that the state of the object is restored.
Observer
Behavioral
Providing a choice for entities to subscribe/unsubscribe from notification
Recipe
- Set up entities,
- Provide a notification hub,
- Provide a way for the hub to unsubscribe entities from notifications.
State
Behavioral
Changing functionality of a class in a maintainable and scalable way
Recipe
- Figure out everything that needs to change about an object,
- Figure out rules on how changes occur,
- Create an interface for objects containing different functionalities,
- Create objects encompassing all possible states,
- Enable states to replace each other as needed,
- Give states reference to the master object,
- Teach master object about its new state mechanism, set up initial state.
Strategy
Behavioral
Flexibility in actions based on circumstances
Recipe
- Create an interface for desired functionality,
- Provide a property of that interface to the implementing object,
- Set up a way (using a Factory, for example) to provide different functionality based on circumstances.
Template Method
Behavioral
Allowing subclasses to vary a common algorithm
Recipe
- Declare a parent class,
- Create a common procedure that calls on all other subprocedures,
- Extend the class, alter subprocedures as needed,
- Call on the parent class' method to execute the main method.
Visitor
Behavioral
Separating algorithms from objects on which they operate
Recipe
- Introduce a place to accept a new functionality,
- Provide a way for a container with new functionality to consume an object.