Gang of Four Design Patterns

GoF Design Patterns

Over 20 years ago the iconic computer science book “Design Patterns: Elements of Reusable Object-Oriented Software” was first published. The four authors of the book: Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, have since been dubbed “The Gang of Four”. In technology circles, you’ll often see this nicknamed shorted to GoF. Even though the GoF Design Patterns book was published over 20 years ago, it still continues to be an Amazon best seller.

The GoF wrote the book in a C++ context but it still remains very relevant to Java programming. C++ and Java are both object-oriented languages. The GoF authors, through their experience in coding large scale enterprise systems using C++, saw common patterns emerge. These design patterns are not unique to C++. The design patterns can be applied in any object oriented language.

These GoF design patterns lay the foundation of core design patterns in programming. They provide a standardized approach to solving recurring design problems, which is why they are considered foundational. Many other design patterns have been developed on top of these foundational patterns to address specific requirements, further emphasizing their importance in the programming world.

By understanding and applying these patterns, developers can create more efficient, scalable, and maintainable code. This is why the GoF design patterns continue to be a crucial part of programming education and practice, influencing countless software solutions across different platforms and languages.

As a Java developer using frameworks like Spring to develop enterprise-class applications, you will encounter the GoF Design Patterns regularly. Here’s a quick breakdown:

  • Creational Patterns: Focus on the creation of objects.
  • Structural Patterns: Define relationships between objects.
  • Behavioral Patterns: Describe how objects interact.

The clarity and utility of these patterns have ensured their place as an indispensable resource for developers worldwide. As you delve into the world of object-oriented programming, these design patterns will become an essential part of your toolkit, guiding you in crafting efficient and maintainable code.

Gang of Four Design Patterns

Creational Design Patterns

Understanding creational design patterns is essential for software developers seeking to manage object creation mechanisms effectively. These patterns address problems related to object instantiation and help make systems more flexible and reusable.

  • Abstract Factory. Allows the creation of objects without specifying their concrete type. This pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes, ensuring that the system is independent of how its objects are created.
  • Builder. Uses to create complex objects. It constructs a complex object step by step, allowing for more flexibility in the creation process. This pattern is particularly useful when the object needs to be created in various configurations.
  • Factory Method. Creates objects without specifying the exact class to create. It defines an interface for creating an object but lets subclasses alter the type of objects that will be created, promoting loose coupling and greater flexibility.
  • Prototype. Creates a new object from an existing object. This pattern is used to create a new object by copying an existing one, which can then be modified as needed. It is particularly effective for objects that are expensive to create from scratch.
  • Singleton. Ensures only one instance of an object is created. By restricting the instantiation of a class to a single object, this pattern ensures that there is a global point of access to that object, making it ideal for shared resources.

These patterns not only streamline the object creation process but also enhance the scalability and maintainability of your software projects. By implementing creational design patterns, you can address common issues around object creation and ensure that your codebase remains robust and adaptable.

Structural Design Patterns

Understanding structural design patterns is crucial for creating flexible and reusable software architecture. These patterns help you manage relationships between entities, making it easier to design systems that are both scalable and maintainable. Let’s explore each of the seven key structural design patterns.

  • Adapter. Allows for two incompatible classes to work together by wrapping an interface around one of the existing classes. This pattern is particularly useful in scenarios where you need to integrate legacy systems with new functionalities without altering their existing codebase.
  • Bridge. Decouples an abstraction so two classes can vary independently. This pattern is often used to separate the interface from the implementation, ensuring that modifications to either aspect do not affect the other. It’s ideal for applications needing scalable interface changes.
  • Composite. Takes a group of objects into a single object. This pattern is perfect for representing part-whole hierarchies, such as a graphics system where shapes like circles and squares can be grouped into a larger diagram.
  • Decorator. Allows for an object’s behavior to be extended dynamically at runtime. This pattern is useful for adding new responsibilities to objects without modifying their structure, providing a flexible alternative to subclassing.
  • Facade. Provides a simple interface to a more complex underlying object. This pattern is beneficial for simplifying interactions with complex subsystems, making it easier for client applications to use them without needing to understand the intricate details.
  • Flyweight. Reduces the cost of complex object models. This pattern is effective in scenarios where you need to manage a large number of similar objects, such as in a string pool, by sharing common data to minimize memory usage.
  • Proxy. Provides a placeholder interface to an underlying object to control access, reduce cost, or reduce complexity. This pattern is commonly used for lazy initialization, access control, or logging, effectively managing resources by acting as an intermediary.

By leveraging these structural design patterns, developers can create robust systems that are easier to maintain and extend over time, ensuring a streamlined approach to software development.

Behavior Design Patterns

Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects. They help manage complex control flows that are difficult to track in traditional object-oriented programming. Here’s a closer look at each pattern:

  • Chain of Responsibility. Delegates commands to a chain of processing objects, allowing multiple objects a chance to handle the request without coupling sender and receiver.
  • Command. Creates objects which encapsulate actions and parameters, promoting loose coupling in a request-response model by separating the request from its execution.
  • Interpreter. Implements a specialized language by defining a grammatical representation and providing an interpreter to process this grammar.
  • Iterator. Accesses the elements of an object sequentially without exposing its underlying representation, offering a standard way to traverse through a group of objects.
  • Mediator. Allows loose coupling between classes by being the only class that has detailed knowledge of their methods, providing a centralized communication medium between different objects in a system.
  • Memento. Provides the ability to restore an object to its previous state without violating encapsulation, enabling the capture and externalization of an object’s internal state.
  • Observer. Is a publish/subscribe pattern which allows a number of observer objects to see an event, making it useful when you need to be notified of changes in the state of an object.
  • State. Allows an object to alter its behavior when its internal state changes, enabling an object to change its behavior when its state changes.
  • Strategy. Allows one of a family of algorithms to be selected on-the-fly at runtime, providing flexibility in algorithm implementation.
  • Template Method. Defines the skeleton of an algorithm as an abstract class, allowing its sub-classes to provide concrete behavior, creating a template method stub and deferring some steps to the subclasses.
  • Visitor. Separates an algorithm from an object structure by moving the hierarchy of methods into one object, used when you need to perform operations on a group of similar objects.

These patterns not only simplify the interaction between objects but also enhance the scalability and maintainability of the system. By understanding and implementing these patterns, developers can create more robust and flexible applications.

Save

53 comments on “Gang of Four Design Patterns

  1. April 28, 2015 at 1:00 am

    When there are only 3 categories why call it GANG OF FOUR?

    Reply
    • April 28, 2015 at 5:14 am

      “Gang of Four” refers to the four authors of the book – Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.

      Reply
    • November 4, 2019 at 11:33 pm

      because these designe patren deovloped by 4 people

      Reply
    • November 15, 2019 at 12:54 am

      No that reason bro..a book called design patterns : elements of reusable oo software was written by 4 persons namely,erich gamma, richart helm, ralph johnson and vlissides..

      Reply
    • February 28, 2020 at 5:58 am

      There are four authors — anyway if that was the question you ask this isn’t the reading material for you

      Reply
      • February 15, 2022 at 6:07 am

        Curiosity is a must for good developers and sarcasm it’s side effect.

        Reply
      • May 2, 2023 at 5:31 am

        There is no such thing as a stupid question and we should encourage people to ask whatever they need to better understand a subject.
        The first part of your answer was constructive, the second part not needed … anyway, if that is your reply to the question this isn’t the answer section for you

        Reply
    • December 3, 2020 at 8:15 am

      Published by Four Authers

      Reply
    • April 30, 2021 at 4:35 am

      Reason : Four People published that Book.

      Reply
    • October 13, 2021 at 8:47 am

      Well, I see it’s kinda a trend here to repeat to this guy that it is 4 authors, not groups of patterns, so …

      Dude, you got it wrong! GoF means 4 authors! Stupid

      Reply
    • January 25, 2023 at 11:59 am

      because the creators of GOF were 4 people

      Reply
    • March 3, 2023 at 3:35 am

      My good sir you are in error… The reference to the “Gang of 4” is a result of there being author count of 4.

      (Because I did not want to be the one to let this thread die…)

      Reply
    • June 16, 2023 at 8:13 pm

      Didn’t get it, how many people exactly created these Design Patterns?

      Reply
  2. March 5, 2018 at 2:15 am

    John, appreciate your efforts in explaining GoF patterns in detail and also with great quality covering OO Design principles, etc.
    Your blog is one of the top best which covers these patterns.

    Thanks for sharing the knowledge.

    Reply
    • March 7, 2018 at 4:36 pm

      Thanks!

      Reply
  3. March 16, 2018 at 5:19 pm

    Thanks John, For explaining Design patterns with excellent examples.

    Reply
  4. March 27, 2018 at 7:54 am

    Give one more i to the visitor in your last line 😉

    Reply
  5. April 12, 2018 at 9:23 am

    Hi John,

    Could you please tell me exactly which patterns are being used in a simple spring application. (Basic CRUD functionality)

    Regards,

    Gavin

    Reply
  6. November 27, 2018 at 10:09 am

    This post keeps me coming back to refresh my knowledge. Thanks so much!

    Reply
  7. December 4, 2018 at 3:14 am

    It’s a great place to learn design patterns for people who want to start implementing scalable applications!!
    Thanks John, really appreciate your effort a lot!

    Reply
  8. December 21, 2018 at 11:41 am

    Finding a design pattern blog post that stitches together ideas has been challenging. This blog has become my go-to since I stumbled upon it. Really love your style of writing. If I’d to add one thing that could be improved, it would be adding pros/cons about the pattern.

    Reply
  9. January 15, 2019 at 5:53 pm

    I have an idea for a GoF Design Patterns spin-off. A good suggested name might be:

    Design Patterns II

    Elements of Reusable Object-Oriented and Functional Javascript
    (Spinoff greatly inspired by Gang-of-Four Design Patterns)

    Inspired by original GoF Design Patterns a 2nd edition would be published citing all 23 or so commonly used or most important design patterns employing only JavaScript or related web development languages where Javascript examples are not appropriate for example CSS variables or serverless databases. A pro edition would also be available for other patterns (perhaps 32 in all) not in the original Design Patterns by GoF. Abstract to concrete real world examples would all be written from Vanilla to Neapolitan flavored Javascript. New edition would make very limited use of jQuery. Examples of in-the-wild popular libraries/frameworks would cite instances where they did it right and where they went wrong. Not a single use of the non-words “foo” and “bar” are printed anywhere in the book as such abstractions are so bland and meaningless that novice programmers often have difficulty grasping the core lessons behind them — novice programmers like me.

    Several outstanding books have already been written for example Learning JavaScript Design Patterns (Volume 1.7.0 is completely free online) by Addy Osmani. But the examples are too bland for me. He should have kade use of more real-world examples especially from frameworks. There is also a great website called refactoring.guru giving one a taste of design patterns (also inspired by GoF) or the full book for a small price but he uses fucking cats and other dumb real-world objects to demonstrate use-cases in UML. UML is too much of an abstraction. Beginners to intermediate programmers need real-world example to really grasp the core ideas.

    My programming skills are far too low to write/co-write or technically review such a book. But if said book were to be published l would be near the top of a very long list to acquire an initial beta release. I just wanted to throw that out there to get your thoughts.

    What do you all think of this idea?

    Reply
    • August 30, 2019 at 12:25 pm

      lmao NO.

      Reply
    • September 29, 2020 at 3:59 pm

      Awesome idea. If “Chicken Soup for the Soul” can make spin-offs for everyone and their mother, why not GoF standards? Heck, make a book for each friking language. Trust me that you don’t really need technical know-how to write that book. You acquire the know-how as you write it… Do it!!

      Reply
      • August 18, 2021 at 6:58 am

        I agree with him

        Reply
    • April 8, 2023 at 12:59 pm

      I like the idea, JS is the language of the future. I am sure open-source frameworks do employ some if not all the design patterns given in the book. You might want to take a look at ExpressJS or ReactJS repo for better examples. Cheers!! Happy Learning!

      Reply
  10. May 22, 2019 at 3:36 am

    Is there any architectural design pattern??

    Reply
  11. December 11, 2019 at 12:43 am

    Thanks for sharing such great knowledge.

    Reply
  12. January 28, 2020 at 5:58 am

    Hi, This simple explanation for all the design patterns helps me a lot to understand.

    Reply
  13. February 29, 2020 at 5:27 am

    In template method pattern, most of the examples show template method as final. Is it not necessary?

    Reply
  14. March 13, 2020 at 3:45 pm

    Hi there. My Firefox Developer Edition browser shows this page with no border nor padding. It would be nicer if the text wasn’t touching the edge of the frame.

    Reply
  15. June 6, 2020 at 6:05 pm

    Woow I’ve worked with several of these patterns already, but I wouldn’t imagine this book was published in 1994. Thought many of the concepts were more recent. Definitely a must read.

    Reply
  16. October 14, 2020 at 6:47 am

    Hello.
    good post. I am interested in web design and your information on design patterns helped me a lot.
    Thanks for sharing this with us.

    Reply
  17. August 10, 2021 at 3:33 am

    I hated the Design Pattern book when it came out because I thought it was tedious (even as a C++ programmer) and too much of it was not as practical (a wise solution) for most frequently encountered problems. Unfortunately, everybody bought too much into it even though it contains many sound ideas. It seemed universally heralded and I believe a great many people bought the book and got nothing out of it. The book was ill-matched for too many that were advised to read it. A small “good parts” book, and more in pseudo-code, would have helped immensely more.

    Reply
  18. July 4, 2022 at 1:29 pm

    Do you ever find that the design patterns have broad applicability in life? I wonder that because business process software replaces work that people would otherwise do. So is it possible that the things we do everyday are best broken down into 22 abstract best practices? Do you find yourself relating to things in this way? Do you think this could be useful?

    Reply
    • July 4, 2022 at 1:32 pm

      Excuse me. *23.

      Reply
  19. August 4, 2022 at 4:24 am

    Hi

    Need to ask a question about Visitor pattern in Behavioral design pattern category. The problem statement was used in the pattern looks quite similar to Bridge Design Pattern.
    In Bridge design pattern:- separated out the sender abstraction and Message abstraction. We used a composition there.
    In Visitor pattern:- Can I use the same ? If no, than please do let me know the reason .

    Reply
  20. December 5, 2022 at 6:58 am

    Can you please share this book in PDF?

    Reply
  21. January 2, 2023 at 12:22 am

    Structural Design Patterns is good one.

    Reply
  22. November 18, 2024 at 6:41 pm

    Great insights on the Gang of Four design patterns! I appreciated how you connected the concepts to the Spring Framework, making it easier to understand their practical applications. It really helped solidify my understanding of software design principles. Thanks for sharing!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.