-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteControlWithUndo.cs
84 lines (68 loc) · 2.28 KB
/
RemoteControlWithUndo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.Text;
namespace Remote
{
//
// This is the invoker
//
public class RemoteControlWithUndo
{
// This time around the remote is going to handle seven On and
//Off Commands which we will hold in the corresponding array and the undocommand.
internal Command[] onCommands;
internal Command[] offCommands;
internal Command undoCommand;
internal Command redoCommand;
public RemoteControlWithUndo()
{
/*In the constructor all we need to do is instantiate the on and off arrays.*/
onCommands = new Command[7];
offCommands = new Command[7];
Command noCommand = new NoCommand();
for (int i = 0;i < 7;i++)
{
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
undoCommand = noCommand;
redoCommand = noCommand;
}
/*The setCommand() method takes a slot position and an On and Off command to be stored commands in the On and Off array for later use*/
public virtual void setCommand(int slot, Command onCommand, Command offCommand)
{
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public virtual void onButtonWasPushed(int slot)
{
onCommands[slot].execute();
undoCommand = onCommands[slot];
}
/* When an On or a Off button is pressed, the hardware takes care of calling the corresponding methods OnButtonWasPushed() or OffButtonWasPushed().*/
public virtual void offButtonWasPushed(int slot)
{
offCommands[slot].execute();
undoCommand = offCommands[slot];
}
public virtual void undoButtonWasPushed()
{
undoCommand.undo();
}
public virtual void redoButtonWasPushed()
{
redoCommand.redo();
}
//Used for testing.
public override string ToString()
{
StringBuilder stringBuff = new StringBuilder();
stringBuff.Append("\n------ Remote Control -------\n");
for (int i = 0; i < onCommands.Length; i++)
{
stringBuff.Append("[slot " + i + "] " + onCommands[i].GetType().FullName + " " + offCommands[i].GetType().FullName + "\n");
}
stringBuff.Append("[redo] " + undoCommand.GetType().FullName + "\n");
stringBuff.Append("[undo] " + undoCommand.GetType().FullName + "\n");
return stringBuff.ToString();
}
}
}