Example #1
0
                $command->execute();
                $this->current += 1;
            }
        }
    }
    public function Undo(int $levels)
    {
        echo "Undo {$levels} levels\n";
        for ($i = 0; $i < $levels; $i++) {
            if ($this->current > 0) {
                $this->current -= 1;
                $command = $this->commands[$this->current];
                $command->unExecute();
            }
        }
    }
    public function compute(string $oper, int $operand)
    {
        $command = new CalculatorCommand($this->calculator, $oper, $operand);
        $command->execute();
        $this->commands[] = $command;
        $this->current = $this->current + 1;
    }
}
$user = new User();
$user->compute('+', 100);
$user->compute('-', 50);
$user->compute('*', 10);
$user->compute('/', 2);
$user->undo(4);
$user->redo(3);