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.