Design patterns are crucial in enhancing code quality, maintainability, and scalability. One of the most influential collections of design patterns is the Gang of Four (GoF) patterns, encapsulated in the seminal book “Design Patterns: Elements of Reusable Object-Oriented Software.”
Table of Contents
What are Design Patterns?
Design patterns are proven solutions to common problems encountered in software design. They provide a structured approach to design and promote code reusability, flexibility, and maintainability. The GoF design patterns are particularly notable for their effectiveness in object-oriented design.
Categories of GoF Design Patterns
The GoF design patterns are categorized into three main groups based on their purpose and functionality:
- Creational Design Patterns: Creational patterns deal with object creation mechanisms, providing flexibility in how objects are instantiated.
- Structural Design Patterns: Structural patterns focus on class and object composition, facilitating the creation of larger structures while keeping them flexible and efficient.
- Behavioural Design Patterns: Behavioral patterns are concerned with communication and interaction between objects, defining how they collaborate to accomplish tasks.
Mnemonic
I just saw pastry with a mic on TV

Creational Design Pattern: Pastry Builder – “P A S Try Builder”
- p: prototype
- as: abstract factory
- try: factory
- Builder: builder
- Singleton
Structural Design Pattern: Fresh Frozen ABCD pastry
- fresh: facade
- Frozen: fly weight
- a: adapter
- b: bridge
- c: composite
- d: decorator
- pastry: proxy
Behavioural Design Pattern: 2 MICS On TV
- m: Memento
- m: Mediator
- i: Iterator
- i: Interpreter
- c: Command
- c: Chain of responsibility
- s: Strategy
- s: State
- o: Observer
- t: Template
- v: Visitor
One Line
- Singleton → Ensuring a class has only one instance and providing a global access point.
- Factory Method → Deferring instantiation to subclasses.
- Abstract Factory → Creating families of related objects without specifying concrete classes.
- Builder → Constructing complex objects step by step.
- Prototype → Cloning objects rather than creating new ones.
- Adapter →Adapts one interface to another
- Bridge → Bridge between Abstraction & Implementation
- Composite → Tree Structure (Part-Whole Hierarchy)
- Decorator → Adds Extra Features
- Facade → Simple Interface to a Complex System
- Flyweight → Reusing Shared Objects
- Proxy → Acts as a Placeholder
- Memento → Snapshot of State
- Mediator → Centralized communication
- Iterator → Sequential Access Without Exposure
- Interpreter → Defining a language grammar
- Command → Encapsulating requests as objects
- Chain of Responsibility → Pass Request Along a handlers
- Strategy → Encapsulating interchangeable algorithms
- State → Changes Behavior When State Changes
- Observer → Notifying multiple objects of changes
- Template Method → Defining a skeleton algorithm in a base class
- Visitor → Defining new operations on objects without modifying them
Real-World Analogies
- Singleton → Like a President (Only one exists at a time)
- Factory Method → Like a Pizza Factory (Can make different types of pizzas without changing the factory)
- Abstract Factory → Like a Vehicle Factory (Can create families of vehicles: bikes, cars, trucks)
- Builder → Like a Subway Sandwich Maker (Step-by-step creation: choose bread, veggies, sauce)
- Prototype → Like a Clone Machine (Copies an existing object instead of creating a new one)
- Adapter → Like a USB to HDMI Adapter (Converts one interface into another)
- Bridge → Like a TV Remote (Abstract buttons can control different TVs)
- Composite → Like a Folder System (Folders can contain files or other folders)
- Decorator → Like Toppings on a Pizza (Adds extra behavior without modifying the base object)
- Facade → Like a Hotel Receptionist (Provides a simple interface to many services)
- Flyweight → Like Chess Pieces (Reuses shared objects instead of creating duplicates)
- Proxy → Like a Security Guard (Controls access to an object, like verifying IDs before entry)
- Memento → Like Ctrl+Z (Undo Button) (Saves & restores object state)
- Mediator → Like an Air Traffic Controller (Manages communication between aeroplanes)
- Iterator → Like a Netflix Next/Previous Button (Sequentially accesses elements without exposing details)
- Interpreter → Like Google Translate (Interprets and processes a defined grammar)
- Command → Like TV Remote Buttons (Encapsulates actions into objects)
- Chain of Responsibility → Like Customer Support Escalation (Passes a request along a chain until handled)
- Strategy → Like Choosing a Payment Method (Credit, Debit, PayPal) (Selects different algorithms at runtime)
- State → Like a Traffic Light (Behavior changes based on state: Red, Yellow, Green)
- Observer → Like YouTube Notifications (Subscribers are notified when a new video is uploaded)
- Template Method → Like a Cooking Recipe (Fixed steps, but ingredients vary)
- Visitor → Like a Tax Auditor (Adds new operations without modifying the object)
Real-world Examples
- Singleton → Logger, Database connection
- Factory → ShapeFactory to create different shape objects
- Abstract Factory → UI frameworks that create buttons & text fields
- Builder → Building a Car object step-by-step
- Prototype → Cloning a document
- Adapter → Charging cable adapter (USB to Type-C)
- Bridge → Remote control for multiple TV brands
- Composite → File system (Folders containing files)
- Decorator → Adding extra toppings to a pizza
- Facade → A single button to start multiple background services
- Flyweight → Using cached character objects in a word processor
- Proxy → Firewall acting as a gateway
- Memento → Undo/Redo functionality
- Mediator → Chat room where users communicate through a central server
- Iterator → Navigating through a playlist
- Interpreter → Google Translate
- Command → Remote control button actions
- Chain of Responsibility → Customer support ticket escalation
- Strategy → Payment methods selection (Credit Card, PayPal, UPI)
- State → Traffic light signals
- Observer → Newsletter subscription
- Template Method → Cooking a recipe with fixed steps
- Visitor → Applying different operations (e.g., tax calculation) on different objects
Definition
Singleton is a creational design pattern that ensures a class can have only one instance and provides a global access point to that instance.
The builder design pattern is a creational design pattern that separates the construction of a complex object from its representation, allowing the same construction process to create various representations.
The prototype design pattern is a creational design pattern that allows the creation of new objects by copying an existing object, known as a prototype, rather than creating new instances using a constructor.
Factory
The factory design pattern is a creational design pattern that defines an interface for creating an object but lets subclasses alter the type of objects that will be created.
Abstract Factory
The abstract factory design pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.
An adapter pattern is a structural design pattern that allows incompatible interfaces to work together by providing a wrapper that converts one interface into another.
The Bridge Design Pattern is a structural pattern that separates an object’s abstraction from its implementation, allowing them to vary independently.
Composite
The composite design pattern is a structural design pattern that composes objects into tree-structured to represent part-whole hierarchies, enabling clients to treat individual objects and compositions uniformly.
The decorator design pattern is a structural design pattern that allows behaviour to be added to individual objects, either statically or dynamically without affecting the behaviour of other objects from the same class.
A facade pattern is a structural design pattern that provides a simplified interface to a complex subsystem, making it easier to use.
The flyweight design pattern is a structural design pattern that uses sharing to support large numbers of fine-grained objects efficiently.
The proxy design pattern is a structural design pattern that acts as a placeholder for another object to control access to it
The memento design pattern is a behaviour design pattern that allows you to capture and restore the internal state of an object without violating encapsulation.
The mediator design pattern is a behaviour design pattern that promotes loose coupling among multiple objects by centralising their communication through a mediator object.
The iterator design pattern is a behavioural design pattern that provides a way to access elements of a collection(or aggregation) sequentially without exposing the underlying representation of the collection.
The interpreter design pattern is a behavioural design pattern that is used to define a grammatical representation of a language and provides an interpreter to deal with this grammar
The Command design pattern is a behavioural design pattern that encapsulates a request as an object, thereby allowing for the parameterization of clients with different requests, queue or log requests and supporting undoable operations.
Chain of responsibility is a behavioural design pattern that allows an object to pass a request along a chain of potential handlers until the request is handled or reaches the end of the chain.\
The strategy design pattern is a behavioural design pattern that enables you to define a family of interchangeable algorithms or strategies and encapsulate each one as an object
The state design pattern is a behavioural design pattern that allows an object to change its behaviour when its internal state changes
The observer design pattern is a behaviour design pattern that establishes a one-to-many dependency between objects. When the state of one object changes. all its dependent objects are notified and updated automatically.
The template method pattern is a behavioural design pattern that defines the outline or structure of an algorithm in a base class but delegates some steps of the algorithms to its base class
The Visitor design pattern is a behavioural design pattern that allows you to add new behaviours to a group of classes without modifying their code. It provides a way to separate algorithms or operations from the object on which they operate.
Benefits of Using GoF Design Patterns
Incorporating GoF design patterns into software development offers several advantages:
- Code Reusability: Patterns provide reusable solutions to common problems, reducing redundant code and promoting modular design.
- Scalability: Patterns facilitate the creation of scalable architectures that can accommodate changes and extensions efficiently.
- Maintainability: By promoting clean and organized code, patterns contribute to easier maintenance and debugging.
- Design Clarity: Patterns improve design clarity by following established best practices and design principles.
Conclusion
The Gang of Four (GoF) design patterns are a cornerstone of object-oriented design, offering time-tested solutions to recurring software design challenges. By leveraging these patterns, developers can create robust, flexible, and maintainable software systems that stand the test of time.
17 thoughts on “Understanding the Gang of Four (GoF) Design Patterns”