AlgoViz

Open/closed principle

medium 55 min

Code should be open for extension and closed for modification: a new kind of thing arrives as new code, and the code that already works stays as it is.

Watch lesson4:49

Where it shows up

The same pattern appears across everyday software.

  • VS Code

    An extension declares contribution points (commands, menus, languages, themes) as JSON in its package.json. The editor reads the declarations and is not edited for an extension.

  • Chrome extensions

    Extensions customise the interface, observe browser events and change pages, without a change of the browser.

  • Express middleware

    A handler gets request, response and `next`. It does its part and passes on, or ends the cycle. If it does neither, the request hangs: a contract with a limit.

  • webpack plugins

    A plugin is an object with an `apply` method that hooks into the compiler. webpack's documentation says that webpack itself is built on the same plugin system.

  • pytest and pluggy

    pytest finds installed plugins through the `pytest11` entry point. pluggy calls hooks last registered first, and `tryfirst` and `trylast` move a hook.

  • Java ServiceLoader

    Loads implementations of a known interface, lazily and from a cache. The caller names only the interface.

  • Python logging

    Loggers give records to handlers, handlers send them to a destination, formatters shape them. A new destination is a new handler, and loggers stay as they are.

  • Kubernetes scheduler

    The scheduling framework is a pluggable architecture: most features are plugins at named extension points, and the core of the scheduler stays light.

  • Stripe dynamic payment methods

    With dynamic payment methods a shop turns a method on or off in its settings, without code. An integration that lists methods in code needs an edit for a new one: both designs on one page.

  • Factorio mods

    Mods extend the game. Load order is decided by depth of dependencies first and by internal name second, so that it is the same everywhere.