Design Patterns — Gang of Four (GOF)

Sachitha S Weerasingha
6 min readDec 18, 2023

--

The Gang of Four (GoF) Design Patterns are a collection of tried-and-tested solutions to common problems in software design. They were first introduced in the book “Design Patterns: Elements of Reusable Object-Oriented Software” in 1994. The authors of this book, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, are collectively known as the “Gang of Four”. These patterns are widely used in the field of software development due to their effectiveness in promoting code reusability and separation of concerns, thereby aiding in the creation of robust and scalable applications.

Design Patterns: Elements of Reusable Object-Oriented Software

GoF Design Pattern Types

The Gang of Four (GoF) Design Patterns are divided into three categories:

  1. Creational: These patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
  2. Structural: These patterns deal with object composition and typically identify simple ways to realize relationships between different objects.
  3. Behavioural: These patterns are specifically concerned with communication between objects.

These patterns are a valuable tool for software developers, as they provide general solutions to common design problems, making software systems more modular, flexible, and maintainable.

Creational Design Patterns

Let’s consider a music festival. The event organizer has to arrange multiple bands, food stalls, security, and other logistics. It’s not just about randomly putting things together. There’s a method to it, a plan. In the world of software, we often need to create complex systems too, just like a music festival. That’s where Creational Design Patterns come in.

Think of Creational Design Patterns as the event organizer’s plan. These patterns help us create objects in a systematic and organized way, just like how an event organizer arranges a successful music festival.

Creational Design Patterns focus on the process of object creation in software development. These patterns ensure that we create systems in a way that’s not only efficient but also flexible, so we can modify them later if needed. They hide the complicated details of how we assemble the components, just like an event organizer who seamlessly arranges various elements to create a memorable experience for the attendees.

There are 5 design patterns in the Creational Design Patterns category.

  1. Singleton Pattern: Ensures that only one instance of a class is created and provides a global point of access to it.
  2. Factory Method Pattern: Allows a class to delegate the instantiation process to subclasses.
  3. Abstract Factory Pattern: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  4. Builder Pattern: Separates the construction of a complex object from its representation so that the same construction process can create different representations.
  5. Prototype Pattern: Allows an object to create customized objects without knowing their class or the details of how they are created.

These patterns are all about class instantiation and can make the software design more flexible, reusable, and understandable.

Structural Design Patterns

Let’s consider a city’s public transportation system. The system includes buses, trams, subways, and taxis. Each mode of transport has its own structure and operates independently, but they all work together to provide efficient transportation for the city’s residents. That’s similar to Structural Design Patterns in software development.

Think of Structural Design Patterns as the blueprint of a city’s public transportation system. These patterns help us organize different classes or objects that have different interfaces (like buses, trams, subways, and taxis) into one larger system (the public transportation system) that can operate smoothly.

Structural Design Patterns focus on how classes and objects are composed to form larger structures. These patterns ensure that if we need to change one part of the system (like replacing buses with electric buses), it won’t disrupt the entire system. They hide the complicated details of how we connect the components, just like a city planner who seamlessly integrates various modes of transport to create an efficient public transportation system.

There are 7 design patterns in the Structural Design Patterns category.

  1. Adapter Pattern: Works like a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.
  2. Bridge Pattern: Decouples an abstraction from its implementation so that the two can vary independently. This type of design pattern comes under structural pattern as this pattern decouples implementation class and abstract class by providing a bridge structure between them.
  3. Composite Pattern: Used where we need to treat a group of objects in a similar way as a single object. This pattern composes objects in term of a tree structure to represent part as well as whole hierarchy.
  4. Decorator Pattern: Allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.
  5. Facade Pattern: Hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities.
  6. Flyweight Pattern: Reduces the number of objects created, to decrease memory footprint and increase performance. This type of design pattern comes under structural pattern as we deal with objects at a low level of system structure.
  7. Proxy Pattern: In a proxy pattern, a class represents the functionality of another class. This type of design pattern comes under structural pattern.

These patterns are all about Class and Object composition. Structural class-creation patterns use inheritance to compose interfaces. Structural object patterns define ways to compose objects to obtain new functionality. They can make the software design more flexible, reusable, and understandable.

Behavioral Design Patterns

Let’s consider a team working on a project. Each team member has a different role, such as project manager, developer, designer, tester, etc. They all have different responsibilities, but they need to communicate and coordinate effectively to complete the project successfully. This is similar to Behavioral Design Patterns in software development.

Think of Behavioral Design Patterns as the communication protocols within a team. These patterns help different objects (like team members) interact with each other and distribute responsibilities among them, just like how a project manager coordinates a team to ensure smooth execution of a project.

Behavioral Design Patterns focus on the communication between objects. These patterns ensure that if we need to change the behavior of one part of the system (like assigning a new task to a developer), it won’t disrupt the entire system. They hide the complicated details of how we manage interactions, just like a project manager who seamlessly coordinates various team members to achieve project goals.

There are 11 design patterns in the Behavioral Design Patterns category.

  1. Chain of Responsibility Pattern: Decouples sender and receiver of a request based on type of request. This pattern comes under behavioral patterns.
  2. Command Pattern: Encapsulates a request as an object, thereby letting users parameterize clients with queues, requests, and operations.
  3. Interpreter Pattern: Provides a way to evaluate language grammar or expression. This type of pattern comes under behavioral patterns.
  4. Iterator Pattern: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  5. Mediator Pattern: Reduces communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loose coupling.
  6. Memento Pattern: Saves and restores the internal state of an object without violating encapsulation.
  7. 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.
  8. State Pattern: Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
  9. 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.
  10. Template Pattern: Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
  11. Visitor Pattern: Lets you define a new operation without changing the classes of the elements on which it operates.

These patterns are all about Class’s objects communication. Behavioral patterns are those patterns that are most specifically concerned with communication between objects.

Conclusion

Design patterns are clever solutions for frequent challenges in creating software. They make it easier to build, update, and adapt programs. The Gang of Four, a team of four specialists, shared these helpful patterns in their well-known book, “Design Patterns: Elements of Reusable Object-Oriented Software.” These patterns guide programmers in crafting software that’s robust and flexible, simplifying the process of managing changes.

--

--