-
Notifications
You must be signed in to change notification settings - Fork 634
/
RemoteControl.cs
50 lines (43 loc) · 1.13 KB
/
RemoteControl.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
namespace CommandPattern
{
internal class RemoteControl
{
private readonly ICommand[] _offCommand;
private readonly ICommand[] _onCommand;
private ICommand _undoCommand;
public RemoteControl(int slots)
{
_onCommand = new ICommand[slots];
_offCommand = new ICommand[slots];
var none = new NoCommand();
_undoCommand = none;
for (var i = 0; i < slots; i++)
{
_onCommand[i] = none;
_offCommand[i] = none;
}
}
public OnOffStruct this[int i]
{
set
{
_onCommand[i] = value.On;
_offCommand[i] = value.Off;
}
}
public void PushOn(int slot)
{
_onCommand[slot].Execute();
_undoCommand = _offCommand[slot];
}
public void PushOff(int slot)
{
_offCommand[slot].Execute();
_undoCommand = _onCommand[slot];
}
public void PushUndo()
{
_undoCommand.Execute();
}
}
}