Exercise 1: A macro_off MacroCommand — Possible Solution ==================================================================== THE NEW COMMANDS, FOLLOWING THIS CHAPTER'S OWN SHAPE ------------------------------ class LightOffCommand: def __init__(self, light): self.light = light def execute(self): self.light.turn_off() def undo(self): self.light.turn_on() class FanOffCommand: def __init__(self, fan): self.fan = fan def execute(self): self.fan.turn_off() def undo(self): self.fan.turn_on() Both mirror this chapter's own LightOnCommand/FanOnCommand exactly, just with execute() and undo() swapped - turning something off is undone by turning it back on, the reverse of the "on" commands this chapter already built. STARTING STATE AND THE NEW MACRO ------------------------------ macro_off = MacroCommand([LightOffCommand(light), FanOffCommand(fan)]) Starting with both light and fan already ON (as they were at the end of this chapter's own macro-on example): after macro_off execute: light.is_on = False , fan.is_on = False call log after execute: ['Living Room Light OFF', 'Ceiling Fan OFF'] Both devices correctly turn off, in the order they were listed in the macro's own command list (light, then fan) - matching this chapter's own macro-on execute() order exactly. VERIFYING THE UNDO, IN REVERSED ORDER ------------------------------ after macro_off undo: light.is_on = True , fan.is_on = True call log after undo: ['Living Room Light OFF', 'Ceiling Fan OFF', 'Ceiling Fan ON (undo)', 'Living Room Light ON (undo)'] Both devices correctly turn back on - and the undo log confirms the fan (turned off second) is undone first, and the light (turned off first) is undone last, exactly the same reversed-order pattern this chapter's own macro-on example demonstrated, now confirmed for a macro built from the opposite pair of commands. WHY THIS WORKS AS AN ANSWER ------------------------------ The new commands are built by mirroring this chapter's own existing on-command classes with execute()/undo() reversed, the macro is verified turning both devices off in the listed order, and its undo is verified reversing them in the opposite order - directly confirming MacroCommand's own reversed-undo behavior isn't specific to the particular on-commands this chapter originally demonstrated it with.