A design pattern is a named, reusable solution to a problem that keeps coming back in software design, with its context and its costs. Patterns are not code to paste: they are a shared vocabulary that lets a team say "use an observer here" and be understood, and they show up whenever objects or functions must cooperate.
Key concepts
- Problem, solution, consequences: a pattern is only a pattern when you know when not to use it.
- Creational patterns: factory, builder and singleton control how objects get made.
- Structural patterns: adapter, decorator, facade and composite shape how objects fit together.
- Behavioural patterns: strategy, observer, command, iterator and state shape how objects share work.
- Principles underneath: program to interfaces, prefer composition to inheritance, keep one responsibility per unit.
Vocabulary
- Gang of Four: the authors of the classic catalogue of object-oriented patterns.
- Anti-pattern: a common response to a problem that makes things worse.
- Dependency injection: passing a part its collaborators instead of letting it create them.
- SOLID: five object-oriented design principles, from single responsibility to dependency inversion.
What to study, in order
- Write a small program with long
ifchains, then replace them with a strategy. - Implement observer and iterator by hand in your main language.
- See which patterns your language makes unnecessary, such as functions as values replacing command.
- Read a mature codebase and name the patterns you find.
- Refactor one pattern you overused back to plain code.
Related tags
#architecture #refactoring #functional-programming #language-design