-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp
More file actions
155 lines (138 loc) · 4.3 KB
/
Copy pathtemp
File metadata and controls
155 lines (138 loc) · 4.3 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Sure, I'd be happy to explain the Command Pattern and provide some examples!
The Command Pattern is a behavioral design pattern that turns a request into a stand-alone object that contains all the information about the request. This transformation allows for parameterizing clients with queues, requests, and operations. It also allows for logging the requests, queuing them, and supporting undoable operations.
Key Components of the Command Pattern:
Command: This is an interface or abstract class that defines the method(s) for executing the command.
Concrete Command: This class implements the Command interface and defines the binding between the Receiver and the action.
Receiver: This is the object that performs the actual action when the command's execute method is called.
Invoker: This class asks the command to carry out the request.
Client: This class creates the ConcreteCommand object and sets its receiver.
Example in C#:
Let's consider a simple example where we have a remote control that can turn a light on and off.
Command Interface:
public interface ICommand
{
void Execute();
}
Concrete Commands:
public class LightOnCommand : ICommand
{
private Light _light;
public LightOnCommand(Light light)
{
_light = light;
}
public void Execute()
{
_light.On();
}
}
public class LightOffCommand : ICommand
{
private Light _light;
public LightOffCommand(Light light)
{
_light = light;
}
public void Execute()
{
_light.Off();
}
}
Receiver:
public class Light
{
public void On()
{
Console.WriteLine("The light is on");
}
public void Off()
{
Console.WriteLine("The light is off");
}
}
Invoker:
public class RemoteControl
{
private ICommand _command;
public void SetCommand(ICommand command)
{
_command = command;
}
public void PressButton()
{
_command.Execute();
}
}
Client:
class Program
{
static void Main(string[] args)
{
Light livingRoomLight = new Light();
ICommand lightOn = new LightOnCommand(livingRoomLight);
ICommand lightOff = new LightOffCommand(livingRoomLight);
RemoteControl remote = new RemoteControl();
// Turn the light on
remote.SetCommand(lightOn);
remote.PressButton();
// Turn the light off
remote.SetCommand(lightOff);
remote.PressButton();
}
}
In this example, the LightOnCommand and LightOffCommand classes implement the ICommand interface and encapsulate the actions of turning the light on and off, respectively. The RemoteControl class acts as the invoker that triggers the command, and the Light class is the receiver that performs the actual action.
This pattern is useful for implementing features like undo/redo, logging, and transaction management. If you have any more questions or need further clarification, feel free to ask! 😊
Type Code Sample:
int exitReturnCode = 0;
bool shouldExit = false;
List<string> builtins = ["exit", "echo", "type"];
while (!shouldExit) {
Console.Write("$ ");
string command = Console.ReadLine()!;
string[] parts =
command.Split(' ', StringSplitOptions.TrimEntries |
StringSplitOptions.RemoveEmptyEntries);
if (!ExecuteBuiltIn(parts)) {
Console.WriteLine($"{command}: command not found");
}
}
return exitReturnCode;
bool ExecuteBuiltIn(string[] parts) {
if (builtins.Contains(parts[0])) {
switch (parts[0]) {
case "exit":
if (parts.Length > 1) {
exitReturnCode = int.Parse(parts[1], CultureInfo.InvariantCulture);
}
shouldExit = true;
break;
case "echo":
for (int i = 1; i < parts.Length; i++) {
if (i > 1) {
Console.Write(' ');
}
Console.Write(parts[i]);
}
Console.WriteLine();
break;
case "type":
string? cmd = parts.Length > 1 ? parts[1] : null;
if (cmd == null) {
break;
}
if (builtins.Contains(cmd)) {
Console.WriteLine($"{cmd} is a shell builtin");
} else {
Console.WriteLine($"{cmd}: not found");
}
break;
default:
throw new ShellException($"Unknown builtin {parts[0]}");
}
return true;
} else {
return false;
}
};
internal sealed class ShellException
(string message) : Exception(message);