天天看點

2022行為型模式C#指令模式(Command Pattern)

Command 設計模式将請求封裝為對象,進而允許您使用不同的請求、隊列或日志請求參數化用戶端,并支援可撤消的操作。

UML類圖

2022行為型模式C#指令模式(Command Pattern)

C# 中的結構代碼

#

此結構代碼示範了 Command 模式,該模式将請求存儲為允許用戶端執行或播放請求的對象。

  • using System;
  • namespace Command.Structural
  • {
  • /// <summary>
  • /// Command Design Pattern
  • /// </summary>
  • public class Program
  • public static void Main(string[] args)
  • // Create receiver, command, and invoker
  • Receiver receiver = new Receiver();
  • Command command = new ConcreteCommand(receiver);
  • Invoker invoker = new Invoker();
  • // Set and execute command
  • invoker.SetCommand(command);
  • invoker.ExecuteCommand();
  • // Wait for user
  • Console.ReadKey();
  • }
  • /// The 'Command' abstract class
  • public abstract class Command
  • protected Receiver receiver;
  • // Constructor
  • public Command(Receiver receiver)
  • this.receiver = receiver;
  • public abstract void Execute();
  • /// The 'ConcreteCommand' class
  • public class ConcreteCommand : Command
  • public ConcreteCommand(Receiver receiver) :
  • base(receiver)
  • public override void Execute()
  • receiver.Action();
  • /// The 'Receiver' class
  • public class Receiver
  • public void Action()
  • Console.WriteLine("Called Receiver.Action()");
  • /// The 'Invoker' class
  • public class Invoker
  • Command command;
  • public void SetCommand(Command command)
  • this.command = command;
  • public void ExecuteCommand()
  • command.Execute();
2022行為型模式C#指令模式(Command Pattern)

C# 中的實際代碼

此實際代碼示範了在簡單電腦中使用的指令模式,該電腦具有無限數量的撤消和重做。請注意,在 C# 中,單詞"operator"是一個關鍵字。在字首為字首"@"允許将其用作辨別符。

  • using System.Collections.Generic;
  • namespace Command.RealWorld
  • // Create user and let her compute
  • User user = new User();
  • // User presses calculator buttons
  • user.Compute('+', 100);
  • user.Compute('-', 50);
  • user.Compute('*', 10);
  • user.Compute('/', 2);
  • // Undo 4 commands
  • user.Undo(4);
  • // Redo 3 commands
  • user.Redo(3);
  • public abstract void UnExecute();
  • public class CalculatorCommand : Command
  • char @operator;
  • int operand;
  • Calculator calculator;
  • public CalculatorCommand(Calculator calculator,
  • char @operator, int operand)
  • this.calculator = calculator;
  • this.@operator = @operator;
  • this.operand = operand;
  • // Gets operator
  • public char Operator
  • set { @operator = value; }
  • // Get operand
  • public int Operand
  • set { operand = value; }
  • // Execute new command
  • calculator.Operation(@operator, operand);
  • // Unexecute last command
  • public override void UnExecute()
  • calculator.Operation(Undo(@operator), operand);
  • // Returns opposite operator for given operator
  • private char Undo(char @operator)
  • switch (@operator)
  • case '+': return '-';
  • case '-': return '+';
  • case '*': return '/';
  • case '/': return '*';
  • default:
  • throw new
  • ArgumentException("@operator");
  • public class Calculator
  • int curr = 0;
  • public void Operation(char @operator, int operand)
  • case '+': curr += operand; break;
  • case '-': curr -= operand; break;
  • case '*': curr *= operand; break;
  • case '/': curr /= operand; break;
  • Console.WriteLine(
  • "Current value = {0,3} (following {1} {2})",
  • curr, @operator, operand);
  • public class User
  • // Initializers
  • Calculator calculator = new Calculator();
  • List<Command> commands = new List<Command>();
  • int current = 0;
  • public void Redo(int levels)
  • Console.WriteLine("\n---- Redo {0} levels ", levels);
  • // Perform redo operations
  • for (int i = 0; i < levels; i++)
  • if (current < commands.Count - 1)
  • Command command = commands[current++];
  • public void Undo(int levels)
  • Console.WriteLine("\n---- Undo {0} levels ", levels);
  • // Perform undo operations
  • if (current > 0)
  • Command command = commands[--current] as Command;
  • command.UnExecute();
  • public void Compute(char @operator, int operand)
  • // Create command operation and execute it
  • Command command = new CalculatorCommand(calculator, @operator, operand);
  • // Add command to undo list
  • commands.Add(command);
  • current++;
2022行為型模式C#指令模式(Command Pattern)

繼續閱讀