Tutorial

Gangs of Four (GoF) Design Patterns

Published on August 3, 2022
author

Pankaj

Gangs of Four (GoF) Design Patterns

Gangs of Four Design Patterns is the collection of 23 design patterns from the book “Design Patterns: Elements of Reusable Object-Oriented Software”.

Gangs Of Four Design Patterns Book
Gangs Of Four Design Patterns Book

This book was first published in 1994 and it’s one of the most popular books to learn design patterns. The book was authored by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. It got nicknamed as Gangs of Four design patterns because of four authors. Furthermore, it got a shorter name as “GoF Design Patterns”.

GoF Design Pattern Types

GoF Design Patterns are divided into three categories:

  1. Creational: The design patterns that deal with the creation of an object.
  2. Structural: The design patterns in this category deals with the class structure such as Inheritance and Composition.
  3. Behavioral: This type of design patterns provide solution for the better interaction between objects, how to provide lose coupling, and flexibility to extend easily in future.

Creational Design Patterns

There are 5 design patterns in the creational design patterns category.

Pattern Name Description
Singleton The singleton pattern restricts the initialization of a class to ensure that only one instance of the class can be created.
Factory The factory pattern takes out the responsibility of instantiating a object from the class to a Factory class.
Abstract Factory Allows us to create a Factory for factory classes.
Builder Creating an object step by step and a method to finally get the object instance.
Prototype Creating a new object instance from another similar instance and then modify according to our requirements.

Structural Design Patterns

There are 7 structural design patterns defined in the Gangs of Four design patterns book.

Pattern Name Description
Adapter Provides an interface between two unrelated entities so that they can work together.
Composite Used when we have to implement a part-whole hierarchy. For example, a diagram made of other pieces such as circle, square, triangle, etc.
Proxy Provide a surrogate or placeholder for another object to control access to it.
Flyweight Caching and reusing object instances, used with immutable objects. For example, string pool.
Facade Creating a wrapper interfaces on top of existing interfaces to help client applications.
Bridge The bridge design pattern is used to decouple the interfaces from implementation and hiding the implementation details from the client program.
Decorator The decorator design pattern is used to modify the functionality of an object at runtime.

Behavioral Design Patterns

There are 11 behavioral design patterns defined in the GoF design patterns.

Pattern Name Description
Template Method used to create a template method stub and defer some of the steps of implementation to the subclasses.
Mediator used to provide a centralized communication medium between different objects in a system.
Chain of Responsibility used to achieve loose coupling in software design where a request from the client is passed to a chain of objects to process them.
Observer useful when you are interested in the state of an object and want to get notified whenever there is any change.
Strategy Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime.
Command Command Pattern is used to implement lose coupling in a request-response model.
State State design pattern is used when an Object change it’s behavior based on it’s internal state.
Visitor Visitor pattern is used when we have to perform an operation on a group of similar kind of Objects.
Interpreter defines a grammatical representation for a language and provides an interpreter to deal with this grammar.
Iterator used to provide a standard way to traverse through a group of Objects.
Memento The memento design pattern is used when we want to save the state of an object so that we can restore later on.

Conclusion

Gangs of Four design patterns lay the foundation of core design patterns in programming. There are many other design patterns built on top of these patterns for specific requirements.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 3, 2021

Sir it’s a fantastic explanation…its a good because its a simple word…

- Salmankhan

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    December 8, 2020

    You are doing a good job for us beginning programmer. Thanks for all. Please ,I’d like to get pdf books about design patterns.

    - emmanuel

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

      New accounts only. By submitting your email you agree to our Privacy Policy

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.