Three Brands, Three Shapes

Three brands of device, three different APIs, and a hub that wants to treat them all the same.

Updated

Plan It, Build It

  1. Meet the Smart-Home Hub: Plan It, Build It (Journeyman): The third pass starts with a smart-home hub that works with one brand of light, behind one front door the phone app and the wall panel call; then two more brands arrive whose code is shaped nothing like the first, and the plan has to keep other companies' code at the edge.

Make It Fit

  1. Adapter: The Interpreter Between Two Strangers (Apprentice): When the hub's routines are written in three brands' words (setPower, release, startStream), every new brand edits every routine; an adapter is a small class that keeps the hub's own promise and passes each call on in the brand's words, so the hub never learns a brand's language.
  2. Abstract Factory: Parts That Must Match (Journeyman): When each brand needs its own connection, command format and status reader, and three separate factories each pick one by the brand's name, one copied line can hand a Bolt lock a Tuneo reader; an abstract factory gives each brand one kit that makes all of its parts, so they can't be mixed.
  3. Choosing a Creational Pattern (Journeyman): By now the course has made objects six ways: a simple factory, a dictionary of functions, Factory Method, Abstract Factory, Builder and Prototype. One look-alike table compares them with plain new, the honest default, so the next object you make gets the way that fits its problem, not the newest pattern you learned.

Two Things That Change on Their Own

  1. Bridge: Any Control, Any Device (Journeyman): Three ways to control the house (the phone app, voice and the wall panel) times three brands of device made nine classes, and every new control or brand adds a whole row of them; a bridge splits the two into separate families, controls that have a device, so a new control or a new brand is one new class.

Exercise: Three Brands, One Hub

  1. Exercise: Three Brands, One Hub (Journeyman): Take the hub's next ticket: Maya's Bolt lock and Tuneo speaker must join the routines, a fourth brand's light must work without editing the hub, and the phone team's remote needs each brand's parts from one kit. Change the working hub and remote so all of it plugs in, with the tests green throughout.