A name for a shape you will see again
After this: Say what a design pattern is and which of the three fits a short description.
On this page · 4 sections · 3 checks
In the last few tutorials you wrote the same arrangement of classes more than once. In "Interfaces and polymorphism in design", a Checkout held a payment method and called pay on it, without asking which kind it was. In "Composition over inheritance", a Vehicle held an engine and asked it for cost_per_km()costPerKm(). The programs were about different things, but the shape was the same: a class holds another object and asks it to do one job.
Programmers noticed long ago that some shapes like this come back in program after program. This tutorial is about three of them.
A shape with a name
When a shape keeps coming back, people give it a name, so they can talk about it in one word. A is such a shape: which classes there are, how they connect, and which problem they solve.
Here is what the name buys. Without it, a programmer in a design meeting has to say:
"Let us write an interface with one method for the delivery fee, one class for each fee rule, and give the order a field that holds one of those classes, handed in from outside."
With it, they say: "Let us make the delivery fee a strategy." Everyone who knows the pattern pictures the same classes and arrows.
A pattern is not a library. There is nothing to install or import. Think of a recipe: it is not food, and you cook it with your own ingredients. A pattern is not code; you write the classes yourself, with the names of your own program.
The three you will meet first
| Pattern | The problem it solves | The shape |
|---|---|---|
| Strategy | One job can be done in several ways, and you want to choose or swap the way | An interface for the job, one class per way, and a field that holds one of them |
| Factory | A name or a setting must become an object of the right class | One method that takes the name and returns the right object |
| Observer | Several objects want to hear when something happens, and they come and go | A list of subscribers, and a call to each one when it happens |
Each of the next three lessons takes one pattern and works through it in the same order:
- a problem you would meet in a real program;
- the shape, as a class diagram;
- one run of it, as a sequence diagram;
- the whole program in Python and Java, which you can run.
Lesson 5 then shows where these patterns already hide in libraries you use, and when a pattern is more than a problem needs.
Check yourself
What is a design pattern?
In a design meeting, a teammate says: "Let us make the delivery fee a strategy." What does the name give the team?
A shop reads a word from each customer's settings (email, sms or push) and must turn it into the right sender object. The choice should be made in one place only. Which of the three patterns fits?
Where the names come from
- Say what a design pattern is: a well-known shape of classes for a problem that keeps coming back, with a name.
- Say why the name helps: one word tells another programmer which classes and arrows you mean.
- Say that a pattern is a shape you fill in with your own names, not code you copy from a library.
- Match the three first patterns to their problems: Strategy swaps how something is done, Factory decides what to make, Observer tells everyone who asked.