Card 08/ 08
Recap: What Design Patterns Are
Four elements, three families, the principle underneath, and the cost of using one unasked.
A design pattern is a named solution to a recurring design problem, a shape of classes to write in your own language rather than code to install.
- Four elements. The Gang of Four describe each of their 23 patterns by its name, the problem it addresses, the solution's classes and responsibilities, and its consequences.
- Recurring means recognisable. The iterator behind a Python
forloop, a Java for-each and a TypeScriptfor...ofis one pattern with the same two roles in three syntaxes. - Three families. Creational patterns decide how objects are made, structural patterns how they fit together, behavioural patterns how they divide work. An adapter is structural even though it changes what a call does.
- Principles underneath. Most patterns apply a SOLID principle to a common problem: Strategy is open/closed, Adapter and Observer are dependency inversion, Composite relies on Liskov substitution.
- Python absorbs some. First-class functions turn a one-method Strategy or Command into a function, generators replace a hand-written Iterator, and a module can be a Facade. The pattern's roles are still there, without the ceremony.
- Every pattern costs something. Use one when the change it protects against has happened or is expected, and write the plain version until then.
← → move through sections and cards