GoF (Gang of Four) Design Pattern

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:

  1. Singleton: ensures that a class has only one instance, and provides a global point of access to it.
  2. Factory Method: defines an interface for creating objects, but allows subclasses to decide which class to instantiate.
  3. Abstract Factory: provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  4. Builder: separates the construction of a complex object from its representation, allowing different representations to be created using the same construction process.
  5. 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:

  1. Adapter: converts the interface of a class into another interface that clients expect, allowing classes with incompatible interfaces to work together.
  2. Bridge: decouples an abstraction from its implementation, allowing the two to vary independently.
  3. Composite: composes objects into tree structures to represent part-whole hierarchies, allowing clients to treat individual objects and compositions of objects uniformly.
  4. Decorator: attaches additional responsibilities to an object dynamically, providing a flexible alternative to subclassing for extending functionality.
  5. Facade: provides a simplified interface to a complex subsystem, making it easier to use and reducing the coupling between subsystems.
  6. Flyweight: reduces the memory footprint of large numbers of similar objects by sharing common parts between them.
  7. 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:

  1. 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.
  2. Command: encapsulates a request as an object, allowing it to be parameterized with different requests, queued, and logged, and supporting undoable operations.
  3. Interpreter: defines a grammar for a language and provides an interpreter to interpret sentences in the language.
  4. Iterator: provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  5. Mediator: defines an object that encapsulates how a set of objects interact, promoting loose coupling between them.
  6. Memento: captures and externalizes an object’s internal state, allowing it to be restored later without violating encapsulation.
  7. Observer: defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.
  8. State: allows an object to alter its behavior when its internal state changes, making it appear to change its class.
  9. Strategy: defines a family of algorithms, encapsulates each one, and makes them interchangeable, allowing them to be selected dynamically at runtime.
  10. Template Method: defines the skeleton of an algorithm in a method, allowing subclasses to redefine certain steps of the algorithm without changing its structure.
  11. 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.