Exercise: Three Brands, One Hub
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.
Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated
Maya's new devices are on the wall, and the hub doesn't know what to do with them. The hub works today, with one brand. Your job is to let two more brands in, and get it ready for a fourth, without changing a line of any brand's own code, and without breaking what works.
Before you start
You need one language set up, as in Setup: VS Code and Your Language, and to know how to run the tests, as in Running the Exercise Tests. The exercise is written in all five of the course's languages; pick the one you use.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-dp-device-adapters, also linked from the Exercise box beside the lesson. Clone it into your course folder. In a terminal, in that folder:
git clone https://github.com/ask-codey/exercise-dp-device-adapters.git
Git makes a folder, exercise-dp-device-adapters, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the brands' own code: Lumo's light, Bolt's lock and Tuneo's speaker, each with its own method names, which you can't change (the repository's
README.mdnames the files for each language); - the hub's code, as it is today: the hub with its two routines, and the phone app's remote, which sends a command to a device's bridge;
- the tests, which you don't need to change;
- a demo program that sets up Maya's kitchen and front door and runs both routines.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: Maya, and the HomeBase phone team
- Maya's Bolt lock and Tuneo speaker are in. "Good morning" should unlock the Bolt front door and play the news on the Tuneo kitchen speaker, and "leaving home" should lock the door and stop the speaker, the way both routines already switch the Lumo lights. The setup screen adds them with
addBolt(...)andaddTuneo(...).- Glint lights arrive next month. Glint's team will hand us their lights already fitted to the hub's own
Lightpromise (on()andoff()), and the setup screen will add them withaddLight(...). The routines must work with them without anyone editing the hub again.- The phone team's new remote screen sends a command with a brand's kit:
sendWith(kit, device, command). A kit hands out that brand's connection, command format and status reader:LumoKit,BoltKitandTuneoKit, and next month Glint's own kit. Today the remote picks each of the three parts by the brand's name, in three separate places, and the three choices must always agree.Everything that works today has to keep working, and the brands' own code can't change.
addBolt, addTuneo, addLight, sendWith and the three kits are already there, because other code calls them, but they don't do anything yet.
What to do
- Run the tests first. Two pass: they're what the hub does today. Five fail: they're the ticket.
- Read the code and find the smells. Whose words are the hub's routines written in? How many places in the remote say which brand's part to use, and what keeps them in step?
- Change the code in small steps, and run the tests after every step. The two that pass must stay green the whole time.
- Stop when all seven are green and the demo prints what When you're done shows.
The README's Stuck? section names the patterns, if you want them. Try a few minutes without it first.
Check your work
Each language has its own command, run in the terminal in your language's folder (Terminal > New Terminal in VS Code). The repository's README.md has the full list, including the one-time setup for Python (a virtual environment and pytest) and TypeScript (npm install).
| Language | Run the tests | Run the demo |
|---|---|---|
| C# | dotnet test |
dotnet run --project ThreeBrands |
| Java | ./mvnw test (.\mvnw test in PowerShell) |
java src/main/java/Demo.java |
| Python | python -m pytest |
python demo.py |
| JavaScript | npm test |
node demo.js |
| TypeScript | npm test |
node demo.ts |
A failing test's message starts with Not yet: for the ticket's work, or Keep this working: if a change broke something that worked, and says what it expected, for example Not yet: good morning unlocks the Bolt front door (RELEASED), and leaving home locks it (ENGAGED).
Hints
- Start with the routines. What would the hub need to keep, instead of Lumo lights, for any brand's light to fit? The ticket's second item names it.
- Look at the test file's Glint light. Glint's team wrote a small class to fit their light to the hub. Bolt's lock and Tuneo's speaker need the same kind of class, and you're the team writing them.
- The remote's three choices belong together. What if one object, made once per brand, handed out all three parts?
- The by-name
sendcan stay. Once the kits work, it only needs to find the right kit for a name and hand it on. You've seen that shape before, as a dictionary.
When you're done
All 7 tests are green, and the demo prints the same six lines in every language:
Good morning, then leaving home:
kitchen light (Lumo): power on, power off
front door (Bolt): RELEASED, ENGAGED
kitchen speaker (Tuneo): streaming https://tuneo.example/news, halted
By brand name: front door: locked
With Bolt's kit: front door: locked
Nothing in the hub's routines mentions a brand anymore, and the Glint light and Glint's kit, written in the test file, worked without a change to the hub or the remote. The fourth brand is now Glint's team's work, not yours.
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-dp-device-adapters), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-dp-device-adapters.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-dp-device-adapters, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
Two brands that didn't fit now do, and the next brand fits without you. That's the job of the code at the edge: it takes the outside world's shapes, so the inside of the app doesn't have to.