AlgoViz

Command

medium 50 min

A command wraps a request in an object: what to do, with which receiver and parameters.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:52

See it run

Wishes as objects: who holds them, who runs them, what they remember.

Operations

Remote and lamp

Full, off, undo, undo, redo. Follow the field before.

Step 1 of 8

Ready

userPersonremoteRemotedonenoneundonenonefullSetLevellevel100before-offSetLevellevel0before-lampLightlevel0

The lamp is dark. The remote has recipes for full and off. The commands shown faded do not exist yet.

Step 1: Ready

class SetLevel:    def execute(self):        self.before = self.light.level        self.light.set_level(self.level)    def undo(self):        self.light.set_level(self.before)class Remote:    def press(self, slot):        self.run(self.slots[slot]())    def run(self, command):        command.execute()        self.done.append(command)        self.undone.clear()    def undo(self):        command = self.done.pop()        command.undo()        self.undone.append(command)    def redo(self):        command = self.undone.pop()        command.execute()        self.done.append(command)
Watch
before
Result
Exact undo

The remote never calls the lamp.

In the real world

The same command doing real jobs. Pick one and step through it.

A slot that holds a command instead of a recipe.

Step 1 of 3

First press

remoteRemotedonehalfhalfSetLevellevel50before100lampLightlevel50execute()

Level 100 becomes 50, before is 100.

Step 1: First press

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Undo by the opposite

    critical

    The state before is not always the opposite.

    Wrong
    def undo(self):    self.light.set_level(0)
    Right
    def undo(self):    self.light.set_level(self.before)
  • Reading the old value too early

    critical

    Others may change it between making and running.

    Wrong
    def __init__(self, light, level):    self.before = light.level
    Right
    def execute(self):    self.before = self.light.level    self.light.set_level(self.level)
  • Redo not cleared

    critical

    Redo applies a command to a state it was not made for.

    Wrong
    def run(self, c):    c.execute()    self.done.append(c)
    Right
    def run(self, c):    c.execute()    self.done.append(c)    self.undone.clear()
  • Remember before running

    major

    If execute raises, the history holds something that never happened.

    Wrong
    self.done.append(c)c.execute()
    Right
    c.execute()self.done.append(c)
  • Macro undo in forward order

    major

    Two parts on one lamp end at the wrong level.

    Wrong
    for c in self.commands:    c.undo()
    Right
    for c in reversed(self.commands):    c.undo()
  • Redo through run

    major

    run clears the undone stack, so the second redo finds nothing.

    Wrong
    def redo(self):    self.run(self.undone.pop())
    Right
    def redo(self):    c = self.undone.pop()    c.execute()    self.done.append(c)

Interview tips

  • Find the missing arrow

    • Invoker knows commands.
    • Commands know receivers.
    • Never invoker to receiver.
  • Exact undo

    • Read in execute.
    • Restore, do not invert.
    • Start tests from 30, not 0.
  • Recipes

    • A slot makes commands.
    • One press, one object.
    • One object, one memory.
  • Outside the process

    • Id from the sender.
    • Acknowledge last.
    • Kinds in a map.
  • Know when not

    • One operation: a function.
    • No undo, queue or log: a call.
    • Facts: an event.