Software design patterns are like spices in cooking -- correct and dosed application will lead to amazing results. Abusing and overusing is just a recipe for a disaster.

Factory

Creational

Creation of objects that conform to the same interface without specifying concrete classes

Centralize creation of different types of objects as long as they conform to the same interface. Create a separate 'factory' class, pass parameters differentiating what object needs to be created, return an object of an interface.
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

A "layer" above the factory, where creation is centralized, but for groups of objects at a time. Essentially a factory squared, where a created item consists of a set of items that also conform to an interface. Like a soccer team, for example, there's a goalie, defenders, attackers. They may be on different teams (Factory), but in order to create a league, each team must be instantiated. Abstract factory to the rescue.
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

Fine-grained control over a complex process. Different from Visitor because can vary methods and order of execution.
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

Just a neat trick to enable object copying.
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.

When only one of something is needed.
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.

Mechanism connecting two incompatible entities.
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

When something seems to be increasingly complex as it is being developed, think about abstracting it.
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

Setting up classes such that calling the same method works differently on two different classes that implement the same interface.
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

Keep wrapping that burrito in more functionality one tortilla at a time.
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

Sweep a mess under one big rug.
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

Meh, don't worry about it, memory is cheap.
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

Kind of a Decorator, but with more distinct purposes.
Recipe: Same as decorator: create an interface, extract functionality, create new entity implementing the interface with new functionality, and so on...

Chain of Responsibility

Behavioral

Standardizing similar separate functionality

A set up for series of checks on an item. Checks must happen sequentially, but not necessarily from the beginning.
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

Approach a task from the other way around -- empower an object with methods and resources it needs to perform and undo actions. Inject access to databases, external resources -- whatever it takes. Then use the command object as a standalone unit that can do stuff.
Recipe: Create an object and make it implement methods in ICommand interface: void execute(), boolean canExecute() (optional), void undo() (optional).

Interpreter

Behavioral

Setting up grammar, sentence structure, and mechanism(s) for translating

Rails for converting something into another format
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

Another layer of indirection ensuring control over how elements of the collection are traversed
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

Offload communication among objects to a central 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

Extracting state of an object on each change in such a way that the object can, if the mechanism is set up, be restored to previous state.
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

A way to control who receives notifications.
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

Abstracting changing state into an interface, working with different objects of the same interface depending on circumstances.
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

Like "State", abstracting different functionality, but here it's a one-time deal and functionalities are not aware of each other
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

Setting up a roadmap of actions, while making arbitrary actions optional
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

Passing items back and forth instead of making a mess in an existing item.
Recipe
  • Introduce a place to accept a new functionality,
  • Provide a way for a container with new functionality to consume an object.