CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 2 of 7 · 1 min read

What a pattern is

A named solution to a repeating problem.

Think of a carpenter's joints. A dovetail joint is a known answer to the problem of joining two boards so they do not pull apart. The carpenter does not invent it each time. Nor does the carpenter use a dovetail where a nail would do.

GoF describes a design pattern as a name, a problem, a solution and consequences. The solution is not finished code. It is a general arrangement of classes that you adapt. The consequences matter as much as the solution, because every pattern trades something: more classes, more indirection, or less flexibility somewhere else.

HF makes a point that suits design rounds. Patterns are tools, and the principles come first. The principles of Level 2 (single responsibility, open-closed, depend on abstractions, favour composition) are the reasons that patterns work. A pattern used without the reason behind it is decoration.

KISS and YAGNI (Level 2, module 7) apply here. HF advises considering the simpler solution before reaching for a pattern, and using one only when you are sure that it addresses a real problem. A design that uses no pattern, and is clear, is a good design.

Design pattern

A design pattern is a named, reusable arrangement of classes that solves a problem that keeps coming up, together with a description of what it costs.

Example: Strategy, which solves "the same job done in several ways" at the cost of one more class per way.

Check yourself: 1. What four parts does GoF give each pattern? 2. Why do principles come before patterns?