1 class Command
2 {
3 public:
4 virtual ~Command() {}
5 virtual void execute() = 0;
6 virtual void undo() = 0;
7 };
8 class MoveUnitCommand : public Command
9 {
10 public:
11 MoveUnitCommand(Unit* unit, int x, int y)
12 : unit_(unit),
13 xBefore_(0),
14 yBefore_(0),
15 x_(x),
16 y_(y)
17 {}
18
19 virtual void execute()
20 {
21 // Remember the unit's position before the move
22 // so we can restore it.
23 xBefore_ = unit_->x();
24 yBefore_ = unit_->y();
25
26 unit_->moveTo(x_, y_);
27 }
28
29 virtual void undo()
30 {
31 unit_->moveTo(xBefore_, yBefore_);
32 }
33
34 private:
35 Unit* unit_;
36 int xBefore_, yBefore_;
37 int x_, y_;
38 };