Routines and Messages

"Good morning" turns on the lights, unlocks the door and starts the news; the devices shouldn't need to know about each other.

Updated

Passing It Along

  1. Chain of Responsibility: The Coin Sorter (Journeyman): When every voice command has to pass a night-time check, a someone's-home check and a permission check, one function full of ifs grows with every new check and gets copied for every new device; a Chain of Responsibility makes each check its own link that stops the command or passes it on, so checks can be added, reordered and reused.

One Hub in the Middle

  1. Mediator: The Hub in the Middle (Journeyman): When the front door has to turn off the alarm, turn on the hallway light and stop the music itself, every device knows several others and every new rule means opening a device's class; a Mediator lets each device tell the hub what happened, and only the hub knows who reacts.

Messages

  1. Observer the Modern Way: Events and Listeners (Journeyman): When every device writes Pass 1's listener list again by hand, and a closed phone screen keeps hearing every change because nothing takes it off the list, it's time for each language's own tools: C#'s event, JavaScript's EventTarget, and a subscribe that hands back a way to unsubscribe.
  2. Publish/Subscribe: Observer Across the Network (Freelancer): When the front door has to tell Maya's phone, which is on another network and sometimes offline, Observer's list breaks down: the door must know the phone, wait for it, and loses the message when it's away; publish/subscribe puts a message board with topics in the middle, so senders post, readers subscribe, and neither knows the other.

Back to How It Was

  1. Memento: Movie Night, Then Back to Normal (Journeyman): When movie night saves each light's brightness itself, by reading the light's fields, then puts them back, it breaks the day the lights gain a color setting; a Memento lets each light make a snapshot of its own state and restore from it, while movie night only holds the snapshots.

Exercise: The Good-Morning Routine

  1. Exercise: The Good-Morning Routine (Journeyman): Take the next ticket for the hub: new porch blinds must react whenever the front door locks or unlocks, and a vacation mode must add a check to every voice command. Change the working hub so both plug in from outside, without editing the front door for each new device or the voice handler for each new check, with the tests green throughout.