GOF (Gang of Four) design patterns refer to a set of 23 software design patterns that were first introduced in the book “Design Patterns: Elements of Reusable Object-Oriented Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. These patterns are considered to be the foundation of modern software engineering, and they provide developers with a common vocabulary and best practices for solving common problems in software design.
The 23 GOF design patterns are categorized into three groups: creational, structural, and behavioral patterns.
Creational patterns
These patterns provide ways to create objects in a flexible and efficient manner. The five creational patterns are:
- Singleton: ensures that a class has only one instance, and provides a global point of access to it.
- Factory Method: defines an interface for creating objects, but allows subclasses to decide which class to instantiate.
- Abstract Factory: provides an interface for creating families of related or dependent objects without specifying their concrete classes.
- Builder: separates the construction of a complex object from its representation, allowing different representations to be created using the same construction process.
- Prototype: specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype.
Structural patterns
These patterns deal with object composition and provide ways to structure objects in a flexible and efficient manner. The seven structural patterns are:
- Adapter: converts the interface of a class into another interface that clients expect, allowing classes with incompatible interfaces to work together.
- Bridge: decouples an abstraction from its implementation, allowing the two to vary independently.
- Composite: composes objects into tree structures to represent part-whole hierarchies, allowing clients to treat individual objects and compositions of objects uniformly.
- Decorator: attaches additional responsibilities to an object dynamically, providing a flexible alternative to subclassing for extending functionality.
- Facade: provides a simplified interface to a complex subsystem, making it easier to use and reducing the coupling between subsystems.
- Flyweight: reduces the memory footprint of large numbers of similar objects by sharing common parts between them.
- Proxy: provides a surrogate or placeholder for another object to control access to it.
Behavioral patterns
These patterns deal with communication between objects and provide ways to manage complex communication patterns in a flexible and efficient manner. The eleven behavioral patterns are:
- Chain of Responsibility: avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
- Command: encapsulates a request as an object, allowing it to be parameterized with different requests, queued, and logged, and supporting undoable operations.
- Interpreter: defines a grammar for a language and provides an interpreter to interpret sentences in the language.
- Iterator: provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Mediator: defines an object that encapsulates how a set of objects interact, promoting loose coupling between them.
- Memento: captures and externalizes an object’s internal state, allowing it to be restored later without violating encapsulation.
- Observer: defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.
- State: allows an object to alter its behavior when its internal state changes, making it appear to change its class.
- Strategy: defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing them to be selected dynamically at runtime.
- Template Method: defines the skeleton of an algorithm in a method, allowing subclasses to redefine certain steps of the algorithm without changing its structure.
- Visitor: defines a new operation to a class without changing the class, by separating the operation from the class and putting it in a separate class.