Exercise: The Dispatch Board
Take the dispatch desk's ticket: Undo and Redo buttons, and actions the planning team writes on its own. Change the working dispatch board so every action can be logged, undone and redone, with the tests green throughout.
Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated
Lakeside Parcel's dispatcher assigns packages to drivers all day, and every mistake sends a driver somewhere for nothing. The dispatch desk wants Undo and Redo buttons, and the planning team wants to hand the dispatcher actions of its own. The code that's there works; your job is to change it so both things work, without breaking anything that works today.
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-dispatch-board, 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-dispatch-board.git
Git makes a folder, exercise-dp-dispatch-board, with the exercise code inside.
The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:
- the dispatch code, as it is today: the board that knows which driver has which package, the dispatcher that the buttons call, and the promise the planning team writes its actions against (the repository's
README.mdnames the files for each language); - the tests, which you don't need to change, with the planning team's first action, "Unassign package", written inside them;
- a demo program that runs a morning at the dispatch desk.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: the dispatch desk, Lakeside Parcel
- Undo and Redo buttons. The dispatcher assigns packages to drivers all day, and a mistake costs a driver a wasted trip. She needs Undo, as many times as she likes, newest first, and Redo to put back what she undid. Once she assigns something new, there's nothing left to redo. Every undo and redo goes in the log ("Undo: Assign package 42 to Leo"), and when there's nothing to undo or redo, the log says so ("Nothing to undo", "Nothing to redo").
- The planning team's own actions. The planning team is writing actions of its own (the first is "Unassign package 42") and will hand them to the dispatcher with
dispatcher.run(action). Each one has three methods:execute,undoanddescribe. An action they write must run, go in the log, and be undone like any other, without anyone editing the dispatch code for it.Everything that works today has to keep working: the Assign button, and the log of every assignment.
run, undo and redo are already there, because the buttons and the planning team's code call them, but they don't do anything yet.
What to do
- Run the tests first. Four pass: they're what the dispatch board does today. Four fail: they're the ticket.
- Read the code and find the smells. Once the Assign button has run, what's left that knows what it did? What would the dispatcher need to take it back?
- Change the code in small steps, and run the tests after every step. The four that pass must stay green the whole time.
- Stop when all eight are green and the demo prints what When you're done shows.
The README's Stuck? section names the pattern, if you want it. 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 Dispatch |
| 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: three undos take package 42 back from Leo to Dana.
Hints
- Look at the planning team's action in the tests. It already has the three methods the ticket asks for. What if an assignment were an object like it?
- An assignment can't take itself back unless it knows who had the package before. When is the right moment to find out?
- Undo needs the newest action first, and so does redo. Which collection always hands back the last thing you put in?
- If the Assign button and
rungo the same way, the planning team's actions get undo for free.
When you're done
All 8 tests are green, and the demo prints the same six lines in every language:
Assign package 42 to Dana
Assign package 43 to Leo
Assign package 42 to Leo
Undo: Assign package 42 to Leo
Redo: Assign package 42 to Leo
Board: package 42 with Leo, package 43 with Leo
The dispatcher can take back as many mistakes as she makes, and the planning team's next action (a "swap", say) will be undone by the same code, without anyone editing the dispatcher.
That's the end of Pass 2. The package tracker grew statuses, routes, carriers, shipments with many parts and an undo button, and you met the patterns teams found for each. Pass 3, Talking to Other Systems, builds the HomeBase smart-home hub: one hub that has to work with lights, locks and speakers from three brands, each with its own way of doing things, and with SkyCast, a weather service that's far away, slow and sometimes down.
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-dispatch-board), 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-dispatch-board.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-dispatch-board, 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
An action that's only a method call is gone the moment it runs. Make it an object that knows how to run, describe and take itself back, and undo, redo, logs and other teams' actions all go through the same few lines.