06 Dec 2023
The Gang of Four (GoF) design patterns refer to a set of 23 classic software design patterns introduced by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides in their influential book titled "Design Patterns: Elements of Reusable Object-Oriented Software." The book, published in 1994, has become a cornerstone in the field of software design and development.
The Gang of Four design patterns are categorized into three main groups:
-
Creational Patterns:
- Singleton Pattern: Ensures a class has only one instance and provides a global point to that instance.
- Factory Method Pattern: Defines an interface for creating an object, but leaves the choice of its type to the subclasses, creating an instance of one of the many possible classes.
- Abstract Factory Pattern: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
- Builder Pattern: Separates the construction of a complex object from its representation, allowing the same construction process to create various representations.
-
Structural Patterns:
- Adapter Pattern: Allows the interface of an existing class to be used as another interface.
- Bridge Pattern: Separates an object’s abstraction from its implementation so that the two can vary independently.
- Composite Pattern: Composes objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.
- Decorator Pattern: Attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
- Facade Pattern: Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
- Flyweight Pattern: Uses sharing to support a large number of similar objects efficiently.
-
Behavioral Patterns:
- Chain of Responsibility Pattern: Passes requests 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 Pattern: Encapsulates a request as an object, thereby allowing for parameterization of clients with different requests, queuing of requests, and logging of the interface.
- Interpreter Pattern: Defines a grammar for interpreting the sentences in a language and provides an interpreter to interpret the sentences.
- Iterator Pattern: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Mediator Pattern: Defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly and allows their interaction to be varied independently.
- Memento Pattern: Captures and externalizes an object’s internal state so that the object can be restored to this state later.
- Observer Pattern: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- State Pattern: Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
- Strategy Pattern: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
- Template Method Pattern: Defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.
- Visitor Pattern: Represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
These design patterns provide general solutions to common problems encountered in software design, promoting flexibility, reusability, and maintainability in object-oriented systems. Developers often use these patterns as best practices to solve recurring design challenges and improve the overall structure and efficiency of their code.