๐Ÿ Design Patterns with Python
Home Chapters Examples Diagrams Bibliography GitHub โ†—
  1. Home
  2. Selecting and Varying Behaviour
Part 5 4 chapters

Selecting and Varying Behaviour

Strategy as a callable, Template Method and its function alternative, State and finite state machines, and Command with undo and command queues.

Chapter 12

Strategy: the callable

Strategy's force survives while its structure dissolves into a callable type, a read-only mapping and a call, which leaves selection, configuration and what a quote records about the rule that priced it.

Read โ†’
Chapter 13

Template Method and the function alternative

Template Method straddles: the hook structure dissolves into a function that takes hooks, and the inheritance form survives where a family of variants shares state and code across steps.

Read โ†’
Chapter 14

State and finite state machines

State survives because no feature says which transitions are legal; a read-only table, frozen states with a match ending in assert_never, and a seeded transit model make the lifecycle short and checkable.

Read โ†’
Chapter 15

Command, undo, and command queues

Command survives where undo needs data a deferred call never kept: frozen amendment values, an inverse computed when the amendment runs, undo and redo stacks and a queue that applies all or nothing.

Read โ†’
โ† Previous part The Python Substrate Next part โ†’ Creating Objects Under Control
Design Patterns with Python
Prose, diagrams and decks: CC BY 4.0. Sample code: Apache 2.0.
Source on GitHub โ†—  ยท  File an issue โ†—