CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 6 of 6 · 2 min read

Exercises

Easy

Easy 1: Name the roles (20 minutes)

In a text editor with a Bold button, name the command, receiver, invoker and client.

Done when: four roles each have a named class.

Hint

The Bold button is the invoker.

Easy 2: Run the history (20 minutes)

Type the schedule code. Run the sequence of section 3 and print the schedule after each step.

Done when: you print [09:00, 10:00], [09:00], [09:00, 10:00], [10:00], [09:00, 10:00].

Hint

TreeSet keeps slots sorted.

Easy 3: Why the flag (20 minutes)

Remove the did flag from Book. Book 09:00 twice, undo once, and print the schedule.

Done when: you see the booking vanish, and restore the flag to fix it.

Hint

Without the flag, undo removes what was there before.

Medium

Medium 1: Text editor (45 minutes)

Write a TextBuffer receiver and commands Insert(text) and Delete(count). Support undo and redo.

Done when: a test types "hello", deletes 2, undoes, and ends with "hello".

Hint

Store the deleted text inside the Delete command.

Medium 2: Macro with failure (45 minutes)

Make a macro of three commands where the second throws an exception. Undo the first on failure so nothing is left changed.

Done when: a failing macro leaves the schedule as it was before.

Hint

Keep a list of commands that already ran.

Medium 3: Worker queue (45 minutes)

Put commands in a BlockingQueue and run them from one worker thread. Print the schedule after the queue is empty.

Done when: ten booking commands from two threads all take effect.

Hint

Use take in the worker loop.

Hard

Hard 1: Persistent log (90 minutes)

Write each executed command to a text file as one line. Add a way to read the file and replay it onto an empty schedule.

Done when: a replay rebuilds the same schedule.

Hint

Each line holds a name and its arguments.

Hard 2: Limit the history (90 minutes)

Limit the undo stack to 50 commands. Decide what to do with the oldest, and test the edge.

Done when: the 51st command drops the oldest and undo stops after 50.

Hint

ArrayDeque.removeLast drops the oldest.

Hard 3: Command or lambda (90 minutes)

Build the same remote control with a Command interface and with lambdas. Add undo to each. Compare.

Done when: a table shows lines of code, inspectability and how undo was added.

Hint

A lambda does not store old state unless you capture it.