Video in progress
What is a design pattern, and why does a developer need one?
A pattern is a proven template for a problem that comes up again and again. It isn't ready-made code, but an idea ("in this situation, structure the code like this") and a shared vocabulary for the team.
📖 Answer
Imagine that in a code review you say "let's use Strategy here" and your colleague gets it right away: an interface, several implementations, picked at runtime. That's the first benefit of a pattern: one word instead of a long explanation.
The second benefit is that you don't rediscover problems other people have already run into. For example, putting object creation in one place (Factory), adding behavior to an existing class without changing it (Decorator), cutting an unwanted dependency (Observer).
But a pattern is not a goal. A mistake juniors often make is reading a book and then stuffing a Factory and a Builder into every class. The code gets more complicated, with no benefit. Let the problem show up first, then pick the pattern that fits.
The classic source is the "Gang of Four" (GoF) book, from 1994, with 23 patterns. In Java and Spring many of them are used out of the box, so knowing them also helps you understand the framework.
Sign up to mark studied questions and save your progress.