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 |