Design Patterns Overview
Design Patterns Overview
Section titled “Design Patterns Overview”Design patterns are reusable solutions to commonly occurring problems in software design. They’re not code you copy — they’re templates for solving problems in a particular context.
First catalogued by the “Gang of Four” (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides) in their 1994 book Design Patterns: Elements of Reusable Object-Oriented Software.
Three Categories
Section titled “Three Categories”Creational
Section titled “Creational”Deal with object creation — how objects are instantiated.
| Pattern | Problem Solved |
|---|---|
| Singleton | Ensure only one instance of a class exists |
| Factory Method | Let subclasses decide which class to instantiate |
| Abstract Factory | Create families of related objects |
| Builder | Construct complex objects step by step |
| Prototype | Clone existing objects |
Structural
Section titled “Structural”Deal with object composition — how objects are assembled from parts.
| Pattern | Problem Solved |
|---|---|
| Adapter | Make incompatible interfaces work together |
| Decorator | Add behaviour to objects without subclassing |
| Facade | Simplify a complex subsystem with a unified interface |
| Proxy | Control access to an object |
| Composite | Treat individual objects and groups uniformly |
| Bridge | Decouple abstraction from implementation |
Behavioural
Section titled “Behavioural”Deal with communication between objects — how they interact and distribute responsibility.
| Pattern | Problem Solved |
|---|---|
| Strategy | Define a family of algorithms and make them interchangeable |
| Observer | Notify dependents when an object changes state |
| Command | Encapsulate a request as an object |
| Chain of Responsibility | Pass a request along a chain of handlers |
| Template Method | Define a skeleton algorithm, defer steps to subclasses |
| Repository | Abstract data access from business logic |
| Iterator | Traverse a collection without exposing its structure |
When to Use Patterns
Section titled “When to Use Patterns”Patterns solve recurring problems. Apply them when:
- You recognise the problem they address
- The solution without a pattern would be complex or brittle
- Team members will understand the pattern
Don’t apply patterns speculatively — “I might need this later” leads to over-engineering. YAGNI (You Aren’t Gonna Need It) applies.
Patterns in Modern Code
Section titled “Patterns in Modern Code”Many patterns are built into modern languages and frameworks:
- Observer → React’s
useEffect, event emitters, RxJS - Strategy → function arguments, dependency injection
- Decorator → Python decorators, TypeScript decorators, middleware
- Repository → Entity Framework, Prisma, SQLAlchemy
- Factory → dependency injection containers
Recognising patterns in frameworks helps you use them more effectively.
SOLID Principles (Related)
Section titled “SOLID Principles (Related)”Design patterns often implement SOLID principles:
| Principle | Description |
|---|---|
| Single Responsibility | A class should have one reason to change |
| Open/Closed | Open for extension, closed for modification |
| Liskov Substitution | Subtypes must be substitutable for base types |
| Interface Segregation | Clients shouldn’t be forced to depend on unused interfaces |
| Dependency Inversion | Depend on abstractions, not concretions |