Exercise: Command
This exercise puts the Command pattern into code.
Scenario
A text Editor supports typing and deletion. Every editing action must be reversible: the user
can call undo at any point to roll back the most recent action. The editor maintains an
internal document string that starts empty.
Commands
| Command | Behavior | Output |
|---|---|---|
["type", text] | Append text to the document | (none) |
["delete", n] | Delete the last n characters (clamp at 0 if fewer than n remain) | (none) |
["undo"] | Revert the most recent type or delete; no state change if history is empty | (none) |
["text"] | Output the current document content, or "(empty)" if the document is empty | the document string |
7 type hello type world text undo text delete 5 text
helloworld hello (empty)
After typing "hello" then "world", the document is "helloworld" and text prints that. undo reverts the second type, leaving "hello", which text then prints. delete 5 removes all five characters, and the final text prints "(empty)" because the document is now empty. Note that type, delete, and undo produce no output; only text prints a line.
Your task
Create TypeCommand and DeleteCommand (each implements the Command interface) so the commands produce the output described above. The starter keeps the Command interface, the Editor class, and the run_editor dispatcher, and marks where to add the two concrete command classes with a TODO comment listing each class's constructor and method behavior.