Checkouttotal 100Buttonlisteners 01Checkout(full)2Checkout(sale)soundcountlog1button.subscribe(fn)2button.click()Your codemake_shapefactoryAppcalls read()1make_shape("circle")2make_shape("square")AdapterOldFilehas get_text()1Adapter(old).read()2old.get_text()fullsalefnfnfn"circle""square"CSread()get_text()
Blocks: the code that uses a pattern (mint) and the pattern's own pieces (violet, sand)Cards: two lines of the Python example for each boardMovers: a rule tile, sign-up chips, envelopes, name tags, new objects and a translated callFour boards, one per pattern. On each, the part that moves is the part the pattern keeps separate: the pricing rule that slides in and out of Checkout (Strategy), the envelopes a button sends to everyone on its list (Observer), the object make_shape hands back when your code names only a kind (Factory), and the call that enters an adapter as read() and leaves as get_text() (Adapter). 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).

What a pattern is

  1. A design pattern is a named, reusable solution to a design problem that keeps coming up. The 1994 book Design Patterns, by four authors nicknamed the Gang of Four, catalogued 23 of them. Here are four, one per board.

Four patterns

Using them well

The Python

# step 1/8 · patterns
Design Patterns (1994)  Gamma, Helm, Johnson, Vlissides
23 patterns in three groups:
  creational  5   how objects get made        (Factory Method)
  structural  7   how pieces fit together     (Adapter)
  behavioral  11  how objects share the work  (Strategy, Observer)
each one: a name, a problem, a solution, the trade-offs

Every Python example in the step details runs as written in Python 3 and prints the lines shown after it. 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.