Labs ICT
⭐ Pro Login

Gang of Four Design Patterns

Overview of the 23 classic design patterns from the Gang of Four book.

The Gang of Four

The Gang of Four (GoF) β€” Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides β€” published "Design Patterns: Elements of Reusable Object-Oriented Software" in 1994. This book catalogued 23 classic design patterns that have become the vocabulary of OO design.

Design patterns are proven solutions to common design problems. They're not code you copy-paste β€” they're templates for solving recurring issues in a disciplined way.

The 23 GoF Patterns

Creational Patterns (5) β€” How objects are created
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Pattern         β”‚ Purpose                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Abstract Factoryβ”‚ Create families of related objects  β”‚
β”‚ Builder         β”‚ Construct complex objects step-by-stepβ”‚
β”‚ Factory Method  β”‚ Delegate instantiation to subclassesβ”‚
β”‚ Prototype       β”‚ Clone existing objects             β”‚
β”‚ Singleton       β”‚ Ensure only one instance exists    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Structural Patterns (7) β€” How classes are composed
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Pattern         β”‚ Purpose                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Adapter         β”‚ Convert one interface to another   β”‚
β”‚ Bridge          β”‚ Separate abstraction from impl.    β”‚
β”‚ Composite       β”‚ Treat individual & groups uniformlyβ”‚
β”‚ Decorator       β”‚ Add responsibilities dynamically   β”‚
β”‚ Facade          β”‚ Simplify a complex subsystem       β”‚
β”‚ Flyweight       β”‚ Share common state efficiently     β”‚
β”‚ Proxy           β”‚ Control access to an object        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Behavioral Patterns (11) β€” How objects communicate
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Pattern         β”‚ Purpose                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Chain of Resp.  β”‚ Pass requests along a chain        β”‚
β”‚ Command         β”‚ Encapsulate requests as objects    β”‚
β”‚ Interpreter     β”‚ Define grammar and interpret it    β”‚
β”‚ Iterator        β”‚ Access elements sequentially       β”‚
β”‚ Mediator        β”‚ Centralize complex communications  β”‚
β”‚ Memento         β”‚ Capture and restore state          β”‚
β”‚ Observer        β”‚ Notify dependents of changes       β”‚
β”‚ State           β”‚ Alter behavior when state changes  β”‚
β”‚ Strategy        β”‚ Swap algorithms at runtime         β”‚
β”‚ Template Method β”‚ Define skeleton, override steps    β”‚
β”‚ Visitor         β”‚ Add operations without changing    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

How to Choose a Pattern

Patterns aren't chosen randomly β€” they're solutions to specific problems. Here's a decision guide:

Problem β†’ Pattern

Need exactly one instance?
  β†’ Singleton

Need to create objects without specifying exact class?
  β†’ Factory Method or Abstract Factory

Need to build complex objects step by step?
  β†’ Builder

Need to add behavior to objects dynamically?
  β†’ Decorator

Need to simplify a complex subsystem?
  β†’ Facade

Need to notify many objects when one changes?
  β†’ Observer

Need to swap algorithms at runtime?
  β†’ Strategy

Need to encapsulate requests as objects?
  β†’ Command

Need to treat individual and composite objects uniformly?
  β†’ Composite

Need to adapt one interface to another?
  β†’ Adapter

Pattern Relationships

Patterns often work together. Understanding how they relate helps you choose the right combination:

Common Pattern Combinations:

Factory Method + Strategy
  ── Create objects with Factory, use Strategy for behavior
  PaymentProcessor = PaymentFactory.create(type)
  processor.pay(amount)  // Strategy determines how

Decorator + Composite
  ── Both deal with wrapping/extending objects
  Component decorator = new LoggingDecorator(component)
  Component tree = new Composite(list of components)

Observer + Mediator
  ── Both manage communication between objects
  Observer: objects subscribe to events
  Mediator: central hub coordinates communication

Adapter + Facade
  ── Both simplify interfaces
  Adapter: converts one interface to another
  Facade: provides simple interface to complex subsystem

Template Method + Strategy
  ── Both handle algorithm variation
  Template: override specific steps in a fixed algorithm
  Strategy: swap entire algorithms

Anti-Patterns to Avoid

Just as there are good patterns, there are bad patterns (anti-patterns) that signal design problems:

Common Anti-Patterns:

1. God Object
   One class that does everything.
   β†’ Apply SRP, extract responsibilities.

2. Spaghetti Code
   Unstructured, tangled code with no clear flow.
   β†’ Apply Template Method, Strategy.

3. Golden Hammer
   Using the same pattern everywhere.
   β†’ Use the RIGHT pattern for the problem.

4. Poltergeist
   Classes with only one method that do no real work.
   β†’ Merge into the class that uses it.

5. Lava Flow
   Dead code that no one dares to remove.
   β†’ Delete it. Run tests. Move on.

πŸ§ͺ Quick Quiz

Which GoF pattern provides a way to access elements of a collection sequentially without exposing its underlying representation?