Design patterns
Some design problems come up again and again: swap one algorithm for another, tell many parts of a program that something changed, create an object without hard-coding its class, or make two pieces of code fit that were not written for each other. Design patterns are the named, reusable answers. Watch four of them, Strategy, Observer, Factory and Adapter, each on its own board with a short Python example, and see what each one keeps apart.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01What is a design pattern?
A pattern is a known answer to a common design problem, with a name, so people can reuse the idea and talk about it. It is not a library and not a law.
02Checkout(full).total(100) gives 100 and Checkout(sale).total(100) gives 90. Which pattern is this?
The pricing rule is an algorithm handed to Checkout. Swapping full for sale changes the result while Checkout's own code stays the same: that is Strategy.
03A button keeps a list of functions and calls every one of them when it is clicked. Which pattern is this, and where else do you see it?
Listeners subscribe to the button and are all notified when its state changes. UI event systems work this way: addEventListener signs a listener up for an event.
04Your app calls read(), but a class you must use only has get_text(). What does an adapter do?
The adapter wraps the old object and gives it the interface the app expects. Neither the app nor the old class has to change.
05In Python, when should you add a pattern such as Strategy?
Patterns add structure, and structure has a cost. In Python a function can be passed in as the strategy (sorted(words, key=len)), so reach for more only when you need it.
Glossary
- Design pattern
- A named, reusable solution to a design problem that comes up again and again. A shared idea, not a piece of code to copy.
- Gang of Four
- Gamma, Helm, Johnson and Vlissides, the four authors of the 1994 book Design Patterns, which catalogued 23 patterns.
- Strategy
- A pattern where an algorithm is passed in, so it can be swapped without changing the code that uses it.
- Observer
- A pattern where listeners subscribe to an object and are all notified when it changes. Also called publish-subscribe in many systems.
- Factory
- Code whose job is to create objects, so the caller asks for a kind of thing instead of naming a class. The book's versions are Factory Method and Abstract Factory.
- Adapter
- A small wrapper that gives an object the interface its caller expects, like a travel plug.
- Interface
- The set of methods code can call on an object, such as read(). Two objects with the same interface can be used the same way.
- Over-engineering
- Adding structure for problems you do not have yet. It makes code longer and harder to change.
Notes and sources
- The Gang of Four book went on sale in 1994; its copyright page says 1995. Its examples are in C++ and Smalltalk. The idea of a pattern language came from the architect Christopher Alexander (1977).
- The book has two factory patterns, Factory Method and Abstract Factory. The make_shape function here is the simpler "factory function" many Python programs use; it has the same goal: the caller asks for a kind of thing, not a class.
- In Python, functions are objects you can pass around, and any object with the right method will do (duck typing). So a strategy is often just a function and an adapter can be tiny. Peter Norvig showed in 1996 that 16 of the 23 patterns become simpler or invisible in dynamic languages such as Lisp and Dylan.
- Every Python example in the step details runs as written in Python 3 and prints the lines shown after it.
- Gamma, Helm, Johnson, Vlissides: Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1994)
- Peter Norvig: Design Patterns in Dynamic Languages (1996)
- Python Sorting HOW TO: key functions
- Python Tutorial: Classes
- Python Glossary: duck typing
- MDN: EventTarget.addEventListener()
- Martin Fowler: Yagni
Quiz Unlocks after the last step.