The GoF (Gang of Four) design patterns are 23 patterns that were first described in the book "Design Patterns: Elements of Reusable Software" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Here's the list of all 23 GOF design patterns, along with a brief explanation of each:
Creational Patterns (5)
- Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
- Builder: Separates the construction and representation of an object, allowing you to construct complex objects step-by-step.
- Factory Method: Provides a way to create objects without specifying the exact class of object that will be created.
- Prototype: Creates a new object by copying an existing object.
- Singleton: Ensures a class has only one instance and provides a global point of access to it.
Structural Patterns (7)
- Adapter: Converts the interface of a class into another interface that clients expect.
- Bridge: Separates an object's abstraction from its implementation, allowing them to vary independently.
- Composite: Composes objects in terms of smaller independent objects, allowing you to treat individual and composite objects uniformly.
- Decorator: Allows an object to add additional responsibilities to another object without affecting the existing object's external interface.
- Facade: Provides a simplified interface to a complex system of classes or interfaces.
- Flyweight: Reduces memory usage by sharing instances of large objects, like graphics or text.
- Proxy: Acts as an intermediary between a client and an object, controlling access to the object.
Behavioral Patterns (11)
- Chain of Responsibility: Allows multiple objects to handle a request without having them know about each other's existence.
- Command: Encapsulates a command or action in an object, allowing you to execute it later.
- Interpreter: Interprets or executes a set of rules or grammar defined by an abstract syntax tree (AST).
- Iterator: Provides a way to access the elements of an aggregate object without exposing its underlying representation.
- Mediator: Reduces coupling between classes that communicate with each other, promoting loose coupling and better scalability.
- Memento: Allows you to capture and externalize an object's internal state, allowing it to be restored later.
- Observer: Notifies objects about changes to another object without having a direct reference to the changed object.
- State: Allows an object to change its behavior when its internal state changes.
- Strategy: Defines a family of algorithms and encapsulates each one in a separate class, allowing you to choose between them at runtime.
- Template Method: Provides a way to define a method that can be customized by subclasses.