Gang of Four Design Patterns Cheat Sheet
Design patterns are standard solutions to common problems in software design. The Gang of Four (GoF) design patterns are divided into three categories: Creational, Structural, and Behavioral. Here’s a cheat sheet of the GoF design patterns:
Category | Design Pattern | Description |
---|---|---|
Creational | Abstract Factory | Creates an instance of several families of classes without detailing concrete classes. |
Builder | Separates object construction from its representation, allowing the same construction process to create different representations. | |
Factory Method | Defines a method for creating an object, but allows subclasses to alter the type of objects that will be created. | |
Prototype | Creates new objects by copying an existing object, known as the prototype. | |
Singleton | Ensures a class has only one instance and provides a global point of access to it. | |
Structural | Adapter | Allows objects with incompatible interfaces to collaborate by wrapping its own interface around that of an already existing class. |
Bridge | Decouples an abstraction from its implementation so that the two can vary independently. | |
Composite | Allows clients to treat individual objects and compositions of objects uniformly. | |
Decorator | Adds new functionalities to objects without altering their structure through a type of wrapper. | |
Facade | Provides a simplified interface to a library, a framework, or any other complex set of classes. | |
Flyweight | Minimizes memory usage or computational expenses by sharing as much as possible with similar objects. | |
Proxy | Provides a placeholder for another object to control access, reduce cost, and reduce complexity. | |
Behavioral | Chain of Responsibility | Passes request along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain. |
Command | Turns a request into a stand-alone object that contains all information about the request. This transformation allows you to parameterize methods with different requests, delay or queue a request’s execution, and support undoable operations. | |
Interpreter | Defines a grammatical representation for a language and provides an interpreter to deal with this grammar. | |
Iterator | Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. | |
Mediator | Reduces coupling between classes by moving the communication patterns between objects to a mediator class. | |
Memento | Provides the ability to restore an object to its previous state (undo via rollback). | |
Observer | Creates a subscription mechanism that allows multiple objects to listen and react to events occurring in another object. | |
State | Allows an object to alter its behavior when its internal state changes. The object will appear to change its class. | |
Strategy | Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from the clients that use it. | |
Template Method | Defines the program skeleton of an algorithm in a method, deferring some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm’s structure. | |
Visitor | Lets you define a new operation without changing the classes of the elements on which it operates. |