
الگوی طراحـــــــــی Command
الگوی طراحی Command یک الگوی رایج در برنامهنویسی شیءگرا است که برای جداسازی دستورها یا عملیاتها از فرماندهندهی آنها استفاده میشود. این الگو اجازه میدهد که دستورها به صورت شیءگرا نمایندهسازی شوند و به عنوان پارامترهایی به انواع مختلف برنامهها یا کلاسها انتقال داده شوند.
مفهوم الگوی Command به این صورت است که یک دستور به عنوان یک شیء در نظر گرفته میشود و اطلاعات مربوط به دستور به صورت پارامترها و یا خصوصیتهای شیء دستور ذخیره میشود. این دستور میتواند در کلاسها یا لیستها ذخیره و مدیریت شود و به راحتی اجرا یا لغو شود.
یک مثال واقعی برای الگوی Command مرتبط با یک سیستم ساده پردازش فایلها را در نظر بگیرید:
1. Command (رابط دستور):
1public interface ICommand
2{
3 void Execute();
4 void Undo();
5}
2. ConcreteCommand (کلاسهای دستور خاص):
1public class CopyCommand : ICommand
2{
3 private FileReceiver _receiver;
4 private string _sourceFile;
5 private string _destinationFile;
6
7 public CopyCommand(FileReceiver receiver, string sourceFile, string destinationFile)
8 {
9 _receiver = receiver;
10 _sourceFile = sourceFile;
11 _destinationFile = destinationFile;
12 }
13
14 public void Execute()
15 {
16 _receiver.CopyFile(_sourceFile, _destinationFile);
17 }
18
19 public void Undo()
20 {
21 _receiver.DeleteFile(_destinationFile);
22 }
23}
24
25public class DeleteCommand : ICommand
26{
27 private FileReceiver _receiver;
28 private string _filePath;
29
30 public DeleteCommand(FileReceiver receiver, string filePath)
31 {
32 _receiver = receiver;
33 _filePath = filePath;
34 }
35
36 public void Execute()
37 {
38 _receiver.DeleteFile(_filePath);
39 }
40
41 public void Undo()
42 {
43 // As there is no "Restore" operation in this example, we can't perfectly undo the deletion.
44 // For the sake of simplicity, we can ignore the Undo implementation for this command.
45 }
46}
3. Receiver (کلاس انجام دهندهی دستور):
1public class FileReceiver
2{
3 public void CopyFile(string sourceFilePath, string destinationFilePath)
4 {
5 // Simulate copying the file from the source to the destination.
6 Console.WriteLine($"Copying file from {sourceFilePath} to {destinationFilePath}");
7 }
8
9 public void DeleteFile(string filePath)
10 {
11 // Simulate deleting the file.
12 Console.WriteLine($"Deleting file at {filePath}");
13 }
14}
4. Invoker (فرماندهنده):
1public class FileProcessor
2{
3 private ICommand _command;
4
5 public void SetCommand(ICommand command)
6 {
7 _command = command;
8 }
9
10 public void ExecuteCommand()
11 {
12 _command?.Execute();
13 }
14
15 public void UndoCommand()
16 {
17 _command?.Undo();
18 }
19}
5. Client (کلاینت):
1public class Program
2{
3 public static void Main()
4 {
5 // Creating the receiver
6 FileReceiver receiver = new FileReceiver();
7
8 // Creating the commands
9 ICommand copyCommand = new CopyCommand(receiver, "source.txt", "destination.txt");
10 ICommand deleteCommand = new DeleteCommand(receiver, "fileToDelete.txt");
11
12 // Creating the invoker
13 FileProcessor processor = new FileProcessor();
14
15 // Execute Copy command
16 processor.SetCommand(copyCommand);
17 processor.ExecuteCommand();
18
19 // Undo Copy command (Delete the copied file)
20 processor.UndoCommand();
21
22 // Execute Delete command
23 processor.SetCommand(deleteCommand);
24 processor.ExecuteCommand();
25 }
26}
در این مثال، Command به صورت شیءهایی از کلاسهای `CopyCommand` و `DeleteCommand` نمایندهسازی شده است. این دستورها برای انجام عملیات کپی فایل و حذف فایل به Receiver ارسال میشوند. Invoker، که در این مثال به عنوان فرماندهنده عمل میکند، دستورها را اجرا و یا لغو میکند. همچنین، هر دستور نیز از Undo برای لغو عملیات اجرا شده استفاده میکند.
وقتی برنامه اجرا میشود، خروجی زیر را خواهید داشت:
Copying file from source.txt to destination.txt
Deleting file at destination.txt
Deleting file at fileToDelete.txt
همانطور که مشاهده میکنید، با استفاده از الگوی Command، ما دستورها را جدا از اجرای آنها نمایندهسازی کردهایم و قادر به لغو دستورات نیز هستیم. این امکان باعث میشود که برنامهها انعطافپذیرتر و آسانتر در برابر تغییرات شوند.