-
Notifications
You must be signed in to change notification settings - Fork 31
/
Command.cs
149 lines (125 loc) · 3.87 KB
/
Command.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
namespace DesignPatterns.Behavioral;
public class Command
{
/// <summary>
/// Client
/// </summary>
/// <remarks>
/// Creates the commands and sets its receiver.
/// </remarks>
[Fact]
public void Execute()
{
var maid = new Maid();
var firstHouse = new House();
Assert.Equal(CleanState.Dirty, firstHouse.BathroomState);
Assert.Equal(CleanState.Dirty, firstHouse.BedroomState);
Assert.Equal(CleanState.Dirty, firstHouse.KitchenState);
// The client asks the maid to clean 2 rooms.
maid.ExecuteTasks(new CleanBathroomTask(firstHouse), new CleanKitchenTask(firstHouse));
Assert.Equal(CleanState.Clean, firstHouse.BathroomState);
Assert.Equal(CleanState.Dirty, firstHouse.BedroomState);
Assert.Equal(CleanState.Clean, firstHouse.KitchenState);
var secondHouse = new House();
// The client only wants the bedroom to be cleaned.
maid.ExecuteTasks(new CleanBedroomTask(secondHouse));
Assert.Equal(CleanState.Dirty, secondHouse.BathroomState);
Assert.Equal(CleanState.Clean, secondHouse.BedroomState);
Assert.Equal(CleanState.Dirty, secondHouse.KitchenState);
var thirdHouse = new House();
// The client wants to have the whole house cleaned.
maid.ExecuteTasks(new CleanTheWholeHouseTask(thirdHouse));
Assert.Equal(CleanState.Clean, thirdHouse.BathroomState);
Assert.Equal(CleanState.Clean, thirdHouse.BedroomState);
Assert.Equal(CleanState.Clean, thirdHouse.KitchenState);
}
/// <summary>
/// Command
/// </summary>
/// <remarks>
/// Defines the interface for executing an operation.
/// </remarks>
public interface ITask
{
void Execute();
}
/// <summary>
/// Invoker
/// </summary>
/// <remarks>
/// Handles the commands and determines when they are executed.
/// </remarks>
public class Maid
{
public void ExecuteTasks(params ITask[] tasks)
{
foreach (ITask task in tasks)
{
task.Execute();
}
}
}
/// <summary>
/// Receiver
/// </summary>
/// <remarks>
/// Receives actions via commands.
/// </remarks>
public class House
{
public CleanState BathroomState { get; set; }
public CleanState BedroomState { get; set; }
public CleanState KitchenState { get; set; }
}
public enum CleanState
{
Dirty, // Default state
Clean
}
/// <summary>
/// Concrete Command
/// </summary>
/// <remarks>
/// - Defines a binding between a Receiver object and an action.
/// - Implements Execute by invoking the corresponding operation(s) on Receiver.
/// </remarks>
public abstract class HouseTask(House house) : ITask
{
protected House House { get; } = house;
public abstract void Execute();
}
/// <inheritdoc />
public class CleanKitchenTask(House house) : HouseTask(house)
{
public override void Execute()
{
House.KitchenState = CleanState.Clean;
}
}
/// <inheritdoc />
public class CleanBathroomTask(House house) : HouseTask(house)
{
public override void Execute()
{
House.BathroomState = CleanState.Clean;
}
}
/// <inheritdoc />
public class CleanBedroomTask(House house) : HouseTask(house)
{
public override void Execute()
{
House.BedroomState = CleanState.Clean;
}
}
/// <inheritdoc />
public class CleanTheWholeHouseTask(House house) : HouseTask(house)
{
public override void Execute()
{
House.BathroomState = CleanState.Clean;
House.BedroomState = CleanState.Clean;
House.KitchenState = CleanState.Clean;
}
}
}