Slow and Far Away
The weather service is far away and slow. The hub needs to cache it, log it and check who's allowed to call it, without changing it.
Updated
A Stand-In at the Door
- Proxy: The Stand-In at the Door (Journeyman): When every screen that can unlock the front door has to check for a guest itself, the one that forgets lets a guest in; a proxy is a stand-in that keeps the lock's own interface and controls the way in, so every caller gets the check without writing it. The same shape also delays slow work until it's needed and stands in for a service far away.
More Features, Same Object
- Decorator: Same Phone, More Features (Journeyman): SkyCast is slow and counts every call, so the hub needs today's weather remembered and every ask logged; subclasses for each combination (cached, logged, cached and logged) multiply with every feature, while decorators wrap the weather source in small layers that keep its interface, so features stack in any order without changing SkyCast.
- Decorators the Modern Way: Wrapping a Function (Journeyman): When the hub needs the same cache in front of tomorrow's forecast as in front of today's weather, a decorator class per method multiplies again; a function that takes a function and returns a better one adds the feature to any call, and Python's @ and TypeScript's decorators are language shortcuts for that same move, applied where a function or class is defined.
Look-Alikes
- Three Wrappers, Three Reasons (Journeyman): The hub now has three kinds of class that look the same on paper, each holding one object and passing calls on: TuneoSpeakerAdapter, CachedWeather and GuardedLock. What each is for tells them apart (a new shape, a new feature, or control of the way in), and that decides where the next rule belongs.
Exercise: The Weather Station
- Exercise: The Weather Station (Journeyman): Take Maya's next ticket: SkyCast now bills every call, so the hub must ask it once and share the answer, and guests must stop changing the thermostat. Add both without touching SkyCast's client or the thermostats, with the tests green throughout.