Design Patterns

Introduction

The Gang of Four (GoF) Design Patterns are a set of software design patterns that were introduced by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides in their book "Design Patterns: Elements of Reusable Object-Oriented Software". The GoF patterns are divided into three categories: creational, structural, and behavioral.

Creational Patterns

Creational patterns are concerned with object creation mechanisms, trying to create objects in a manner suitable to the situation. Creational patterns are:

  • Singleton Pattern: Ensures that a class has only one instance, and provides a global point of access to it.

  • Factory Method Pattern: Defines an interface for creating objects, but lets subclasses decide which class to instantiate.

  • 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.

  • Prototype Pattern: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

Structural Patterns

Structural patterns are concerned with object composition, providing ways to associate objects to form larger structures. Structural patterns are:

  • Adapter Pattern: Converts the interface of a class into another interface that clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

  • Bridge Pattern: Decouples an abstraction from its implementation so that the two can vary independently.

  • Composite Pattern: Composes objects into tree structures to represent part-whole hierarchies. Composite 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 large numbers of fine-grained objects efficiently.

  • Proxy Pattern: Provides a surrogate or placeholder for another object to control access to it.

Behavioral Patterns

Behavioral patterns are concerned with object interactions, distributing responsibilities among objects, and controlling the flow of control between objects. Behavioral patterns are:

  • Chain of Responsibility Pattern: Avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

  • Command Pattern: Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

  • Interpreter Pattern: Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

  • 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. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

  • Memento Pattern: Without violating encapsulation, capture and externalize 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

  • Template Method Pattern: Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's 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.

Conclusion

The GoF Design Patterns provide a standardized way of solving common software design problems. They are widely used in object-oriented programming and provide a common vocabulary for discussing design patterns. By understanding these patterns, software developers can improve the quality of their code and create more maintainable, flexible, and scalable software systems.

Last updated