Courses / Machine Coding and Low-Level Design in TypeScript / Chapter 5
Module 3 · Design principles and patterns · Chapter 5 of 28
Design Patterns in TypeScript: Creational, Structural and Behavioural
Builds on Design Principles: SOLID, DRY, KISS, YAGNI and the Law of Demeter. Next up: Solving a Design Problem: Requirements to Tested Code in Six Steps.
Part 1 · What a pattern is and how to choose one
A named solution to a recurring problem
Most design problems you meet at work have been met before, and the useful answers have names. Knowing the name lets you say "an adapter here" in three words instead of drawing the same boxes again.
A design pattern is a named, reusable shape of solution to a problem that keeps appearing in object-oriented design. It is not a library and not code you copy. It describes the roles that objects play, how they talk, and what you give up by choosing it. The book that made the idea standard is Design Patterns by Gamma, Helm, Johnson and Vlissides (1994), whose authors are known as the Gang of Four. It describes 23 patterns in three families, and its list is still the shared vocabulary.
Patterns also have a cost. Each one adds classes, indirection and names for a reader to learn. Used where no such problem exists, a pattern makes code harder to read. The skill is to recognise the problem first and reach for the pattern second.
Three families
The 23 patterns are grouped by the kind of problem they address. Creational patterns are about how objects get created. Structural patterns are about how objects and classes are assembled into larger shapes. Behavioural patterns are about how objects share responsibility and communicate.