15. Design Patterns
Programming Project 2021/22

15.3. Gang of Four Design Patterns

Design patterns gained popularity in computer science after the book "Design Patterns: Elements of Reusable Object-Oriented Software" was published in 1994 by the so-called "Gang of Four" (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides)

There are 23 design patterns which can be classified in three categories.

  • Creational Patterns deal with the process of object creation.
  • Structural Patterns deal with the composition of classes or objects.
  • Behavioral Patterns characterize the ways in which classes or objects interact and distribute responsibility.

Creational Patterns

  • 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 so that the same construction process can create different representations.
  • Factory Method pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method pattern lets a class defer instantiation to subclasses.
  • Prototype pattern specifies the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
  • Singleton pattern ensures that a class has only one instance, and provides a global point of access to it.

Structural Patterns

  • Bridge pattern decouples an abstraction from its implementation so that the two can vary independently.
  • Adapter pattern converts the interface of a class into another interface clients expect. Adapter pattern lets classes work together that couldn’t otherwise due to incompatibility.
  • Composite pattern composes objects into tree structures to represent part-whole hierarchies. Composite pattern lets clients treat individual objects and compositions of objects uniformly.
  • Decorator pattern attaches additional responsibilities to an object dynamically. Decorator pattern provides 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

  • 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. This way, we 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 making it possible to parameterize clients with different requests, queue or log requests.
  • Interpreter pattern given a language, defines a representation for its grammar along with a mechanism 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 pattern promotes loose coupling by keeping objects from referring to each other explicitly to vary their interaction independently.
  • Memento pattern, without violating encapsulation, 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.
  • Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.
  • Template Method pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method pattern 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 pattern lets you define a new operation without changing the classes of the elements on which it operates.