Gang of Four (GoF) Design Pattern

Design PatternGang of Four (GoF) Patterns are 23 main software design patterns providing recurring solutions to common problems in software design. They were developed by  Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, often referred to as the Gang of Four.

We have started this series to explain each of these patterns with a real world example and how to develop and use them in Java.

Creational Design Pattern:

This is all about object creation in a form that they can follow the architecture principle of loose coupling, encapsulation and abstraction. This pattern can provide a great deal of flexibility about which objects are created, how those objects are created, and how they’re initialized. And the most important thing is that the client remains completely unaware of overall object creation process.

 Structural Design Pattern:

This pattern is used to form larger object structures from many different objects.  They show you how to use composition to join different pieces of a system together in a flexible and extensible fashion. This pattern help you guarantee that when one of the parts changes, the entire structure doesn’t need to change. They also show you how to recast pieces that don’t fit into pieces that do fit.

 Behaviour Design Pattern:

This pattern is used to manage relationships, interaction, algorithms and responsibilities between various objects. It also follows the abstraction principle whereby it abstracts the action. This pattern focuses on the interaction between the cooperating objects in a manner that the objects are communicating while maintaining loose coupling.

Creational PatternStructural PatternsBehavioral Patterns
Abstract FactoryAdapterChain of Responsibility
BuilderBridgeCommand
Factory MethodCompositeInterpreter
PrototypeDecoratorIterator
SingletonFacadeMediator
FlyweightMemento
ProxyObserver
State
Strategy
Template Method
Visitor

The below diagram provides a clear view of the relationship between the above design patterns mentioned.

Design Pattern Relationship

Design Pattern Relationship

Types of Creational Design Patterns (as defined by Wikipedia):
Abstract Factory groups object factories that have a common theme.
Builder constructs complex objects by separating construction and representation.
Factory Method creates objects without specifying the exact class to create.
Prototype creates objects by cloning an existing object.
Singleton restricts object creation for a class to only one instance.

Types of Structural Design Pattern (as defined by Wikipedia):
Adapter allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.
Bridge decouples an abstraction from its implementation so that the two can vary independently.
Composite composes zero-or-more similar objects so that they can be manipulated as one object.
Decorator dynamically adds/overrides behaviour in an existing method of an object.
Facade provides a simplified interface to a large body of code.
Flyweight reduces the cost of creating and manipulating a large number of similar objects.
Proxy provides a placeholder for another object to control access, reduce cost, and reduce complexity.

Types of Behavioral Design Pattern (as defined by Wikipedia):
Command creates objects which encapsulate actions and parameters.
Interpreter implements a specialized language.
Iterator accesses the elements of an object sequentially without exposing its underlying representation.
Mediator allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
Memento provides the ability to restore an object to its previous state (undo).
Observer is a publish/subscribe pattern which allows a number of observer objects to see an event.
State allows an object to alter its behavior when its internal state changes.
Strategy allows one of a family of algorithms to be selected on-the-fly at runtime.
Template method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.
Visitor separates an algorithm from an object structure by moving the hierarchy of methods into one object.

5 Responses to “Gang of Four (GoF) Design Pattern”

  1. Hamid Seleman

    Sep 13. 2013

    Hi there,

    You did a great job explaining the design patterns, working code example, justification and interesting points with subtle differences with other design patterns when there is a thin line of differentiation which can lead to confusion.

    I would love to read the rest of missing design patterns’ discussion as of writing this comment such as:

    Creational Pattern – Builder
    Structural – Bridge, Composite, Flyweight
    Behavorial – Other than Command

    thanks a bunch,

    Reply to this comment
  2. Deepak S

    Mar 26. 2014

    Nice, short but very much informative! great job

    Reply to this comment

Trackbacks/Pingbacks

  1. Proxy Design Pattern - Gang of Four | IdioTechie - November 10, 2012

    [...] Proxy Design Pattern Posted by IdioTechie on November 10, 2012 Description: Proxy is another Structural design pattern which works ‘on behalf of’ or ‘in place of’ another object in order to access the [...]

  2. Façade Design Pattern - Design standpoint | IdioTechie - November 22, 2012

    [...] about the Adapter Design Pattern. In today’s article we are going to show about another such Gang of Four Structural patterns.  As the name suggests structural pattern is used to form a larger object structure from many [...]

  3. Singleton Design Pattern An introspection and best practices | IdioTechie - February 18, 2013

    [...] Singleton is a part of Gang of Four design pattern and it is categorized under creational design patterns. In this article we are going to [...]

Leave a Reply