-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
139 lines (116 loc) · 2.96 KB
/
Program.cs
File metadata and controls
139 lines (116 loc) · 2.96 KB
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
using System;
using System.Collections.Generic;
namespace Command;
// Command interface
public interface ICommand
{
void Execute();
void Undo();
}
// Receiver
public class Light(string location)
{
// Using C# 14 field keyword for cleaner implementation
public string Location
{
get;
init => field = value ?? throw new ArgumentNullException(nameof(value));
} = location;
public void TurnOn()
{
Console.WriteLine($"{Location} light is ON");
}
public void TurnOff()
{
Console.WriteLine($"{Location} light is OFF");
}
}
// Concrete Commands
public class LightOnCommand(Light light) : ICommand
{
public void Execute()
{
light.TurnOn();
}
public void Undo()
{
light.TurnOff();
}
}
public class LightOffCommand(Light light) : ICommand
{
public void Execute()
{
light.TurnOff();
}
public void Undo()
{
light.TurnOn();
}
}
// Invoker
public class RemoteControl
{
private ICommand command;
private Stack<ICommand> commandHistory = [];
public void SetCommand(ICommand command)
{
this.command = command;
}
public void PressButton()
{
command.Execute();
commandHistory.Push(command);
}
public void PressUndo()
{
if (commandHistory.Count > 0)
{
ICommand lastCommand = commandHistory.Pop();
lastCommand.Undo();
}
else
{
Console.WriteLine("Nothing to undo");
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Command Pattern Demo ===");
Console.WriteLine();
// Receivers
Light livingRoomLight = new Light("Living Room");
Light kitchenLight = new Light("Kitchen");
// Commands
ICommand livingRoomOn = new LightOnCommand(livingRoomLight);
ICommand livingRoomOff = new LightOffCommand(livingRoomLight);
ICommand kitchenOn = new LightOnCommand(kitchenLight);
ICommand kitchenOff = new LightOffCommand(kitchenLight);
// Invoker
RemoteControl remote = new RemoteControl();
// Execute commands
Console.WriteLine("Turning on living room light:");
remote.SetCommand(livingRoomOn);
remote.PressButton();
Console.WriteLine();
Console.WriteLine("Turning on kitchen light:");
remote.SetCommand(kitchenOn);
remote.PressButton();
Console.WriteLine();
Console.WriteLine("Turning off living room light:");
remote.SetCommand(livingRoomOff);
remote.PressButton();
Console.WriteLine();
Console.WriteLine("Undo last command:");
remote.PressUndo();
Console.WriteLine();
Console.WriteLine("Undo previous command:");
remote.PressUndo();
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}