LevelJar Sign in

Python course / Applied

Patterns and architecture

Give a growing program a shape: a pure core, a thin shell, and pieces you can swap. Most architecture problems start as one class quietly doing three jobs.

  • strategies
  • registries
  • composition
  • pure core
  • repositories
  • state machines
  • undo
  • configuration

Start this module

Runs in your browser. Start straight away; create an account when you want your progress kept.

a registry instead of an if chain
COMMANDS = {}


def command(name):
    def register(func):
        COMMANDS[name] = func
        return func

    return register


@command("add")
def add(basket, item):
    return basket + [item]


@command("clear")
def clear(basket, item):
    return []


basket = []
for name, item in [("add", "tea"), ("add", "jam"), ("clear", None)]:
    basket = COMMANDS[name](basket, item)
    print(name, "->", basket)
add -> ['tea']
add -> ['tea', 'jam']
clear -> []

What you will learn

The lessons in this module

  • A class that should be a function
  • Strategies as functions
  • A registry of commands
  • Composition over inheritance
  • Events: telling whoever is listening
  • A pure core and a thin shell
  • Repositories: storage behind a promise
  • Wiring it together in one place
  • State machines
  • Commands that can be undone
  • Settings checked at the door
  • Errors that belong to your code
  • Refactoring under a safety net
  • When the pattern is the problem
  • Capstone: the cafĂ©
  • Module review: a basket with undo

Ready when you are

Start this module

Previous: Testing. All modules are on the course page.