Open In App

Introduction to Gang Of Four(GoF) | Design Patterns

Last Updated : 31 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The book Design Patterns: Elements of Reusable Object-Oriented Software consists of 23 Design patterns, which are collectively called: “Gang Of Four Design Patterns” (abbreviated as – GoF). The reason why we call it a ‘Gang Of Four’ is because the book is co-authored by the following 4 authors :

Erich Gamma
Richard Helm
Ralph Johnson
John Vlissi

This book popularized the concept of design patterns that serve as a solution for software design problems. And its considered as a classic in modern software engineering literature. It categorized the software design patterns into – Creational, Structural, and Behavioral Design Patterns.

Types of GOF Pattern

There are following 3 categories of Design Patterns that fall under Gang Of Four :

1. Creational

These patterns deal with the object instantiation mechanism typically used to minimize the dependency between them and abstract the procedure of object creation manually making it even more flexible.

2. Structural

The main objective of these patterns is to achieve object composition, effectively manage the highly dependent objects and relationships among them throught different parts of the software application.

3. Behavioral

These patterns are concerned how the objects interact and collaborate with each other to achieve a specific functionality.

Primary Objective of these patterns:

These patterns serve as building blocks for the developers in writing efficient, flexible, and easily maintainable code. They are widely used in industry while writing code for software applications. Each of the design pattern describes a particular problem and provides a specific solution for it.

Following are the primary objectives:

1. Reusability

The reusability principle says that the code previously written shall be usable by other components of our system in different parts of our software helping the developers avoid code redundancy and other bugs that might arise.

2. Maintainability

The maintainability property of the code mainly focuses on the structure of our code, and the flexibility it offers us. The code shall be easily modifiable which can be achieved by dividing our application into various modules each one having a single responsibility. So, if any changes were to be made in any part of our application, it won’t effect other areas.

3. Scalability

The design patterns offers developers with a lot of flexibility while making certain changes so if there are some additional components to be added or any functionality to be removed, then that can be done effectively while following certain design patterns.

4. Communication and collaboration

Design patterns have a common set of vocabulary and their meanings, when developers are familiar with these patterns they can effectively communicate the idea to each other which promotes collaboration and better problem solving skills while also avoiding any misunderstanding.

Creational Design Patterns

Following are very commonly used Creational Design Patterns that assist in creation and manipulation of objects in efficient way.

Creational-Design-Patterns

Creational Design Patterns

Detailed overview on each of these pattern:

Pattern Name

Description

Factory Pattern

This pattern has a single class which will provides us objects on demand merely by invoking a method from our factory class.

Abstract Factory Pattern

It is just another layer of abstraction over the factory pattern which involves creation of multiple logically related objects from a factory class.

Singleton Design Pattern

Singleton as its name indicates, this pattern only allows a ‘SINGLE’ object to be created for a specific class and that object is shared across different parts of the application

Builder Pattern

The aim of this pattern is to allow different representations of an object to be created by the use of same construction process. Basically, the construction and representation parts are separated.

Prototype

This is probably the simplest creational pattern which involves creating a new object from already existing objects and can be achieved through the use of copy constructor

Structural Design Patterns

The criteria here is to put such a system in place where the objects can be composed to new functionalities and interact in easiest way possible despite if they’re incompatible, these pattern allows us to achieve certain level of communication between them.

Structural-Design-Pattern-(1)

Structural Design Pattern

Detailed overview on each of these pattern:

Pattern

Description

Adapter

This pattern is given preference when we need to make two incompatible interfaces work together. for example, if we have an old_system, and we need it to be compatible and operate through new_system then we can utilise this pattern

Bridge

When we have is a relationship between the abstract class and the implementation class then any change in one would effect the other which isn’t a good practice that’s when the bridge pattern comes in , it separates both abstraction and the implementation, now we can extend or modify both independently

Composite

It composes (or groups) multiple objects into one, forming a tree like hierarchy between objects. Key components are :

  1. COMPONENT : An abstract class or interface,
  2. LEAF : The concrete classes that implement the component
  3. COMPOSITE: It is also a concrete class, but it can contain collection of various different child-elements.

Decorator

It has an additional component called – “Decorator” which adds its own functionality either before or after delegating a call.
This pattern basically allows us to alter the behavior of an object statically or dynamically at run-time.

Facade

The core component here is the “facade”, its nothing but a class that intercepts every call made by the client and delegate it to the sub-classes that have the capability to handle the request. Its like a security guard that coordinates the action of the underlying system.

Flyweight

The main focus of this pattern is to boost computation and memory related efficiency by sharing the data across the system and reducing redundant objects.

Example: when we need 1000s of document objects with SAME functionality in the system and whenever a new user joins an object will be created so rather than doing this, you can share one single object across the whole system by only incrementing its value. This reduces the load of creating object very frequently and there by saving memory.

Proxy

Proxy basically means an intermediatory between 2 entities, basically representing another object to restrict/control access to it. Some use cases would be when we require some initial work to be done before processing a request, or adding an additional functionality which not a part of system that lies beyond the proxy.

Behavioral Design Pattern

A scenario, in which Behavioural Design Patterns will amount to be very useful is when we need a system in place where objects need to be as collaborative and interactive as possible inside of a complex system. In these patterns, the responsibilities are given/assigned to various objects.

Behaviorla-Design-Pattern

Behavioral Design Pattern

Detailed overview on each of these pattern:

Pattern Name

Description

Observer

This pattern is useful in scenarios when we have one to many dependency between objects, when the entity on the “one” side of the relationship, then all the instances of the “many” side entity reflect the required change in them automatically.

Iterator

It allows us to simply traverse the elements, without exposing the underlying implementation that may give rise to vulnerabilities.

State

Its basically allowing a particular object to change its behaviors if its state is altered in any way. You can easily visualize it in correspondence with polymorphic behaviour of behaving differently under different circumstances.

We create various different concrete states implementation and behave as whenever the state of an object is altered.

Command

When we need to achieve a framework of loose-coupling rather than hard-coding, then this pattern will be very useful. It does this by treating the request made by the client as a whole object which can be given as an argument to the method or enqueue it for execution (if there are multiple request) additionally, it will also help us in performing undo operations on our requests.

Template

This pattern is solely based on the principle of overriding, that allows us to replace and add new functionalities to our code.
However, what differentiates it from other patterns is that it allows us to define a template so other sub-classes can provide an implementation specific to their own requirements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads