Choosing the Right Pattern
A practical guide to picking a design pattern from the problem you see, telling confusable patterns apart, and knowing when to use no pattern at all.
Imagine a carpenter with a full toolbox: a hammer, a saw, a chisel, a drill. A beginner picks up the tool that looks most impressive. An experienced carpenter looks at the job first: "I need to join two boards", and then reaches for the right tool. Sometimes the right tool is the simple hammer, and sometimes the answer is that no special tool is needed at all.
You have now learnt twelve design patterns. The skill that interviewers test is not whether you can recite them. It is whether you can look at a problem and choose well, or choose none. This lesson gives you a method, a quick guide, a list of patterns that people confuse, and a way of speaking about your choice.
01Start with the pain, not with the pattern
Every pattern in this track was born from a pain: something in the design that hurt. A pattern is only useful when you feel that pain. So the method is:
- Find what is hurting. A growing
if/else? A class that must be edited for every new feature? A web of objects that all know each other? An object that behaves differently in many situations? - Name the pain in plain words. "Pricing rules keep changing." "Many things must react when an order ships."
- Pick the smallest fix that removes the pain. Often it is a pattern. Sometimes it is just a better method or a new class.
- Say the trade-off. Every pattern adds classes and indirection. Name the cost you accept.
If you cannot name the pain, you do not need the pattern yet.
Loading the rest of the lesson…