前言
代码
#region 基础的命令模式
public abstract class Command
{
public abstract void Execute();
}
public class SendCommand : Command
{
private Receiver receiver;
public SendCommand(Receiver receiver)
{
this.receiver = receiver;
}
public override void Execute()
{
receiver.Execute();
}
}
public class Receiver
{
public void Execute()
{
Console.WriteLine("receiver execute the command...");
}
}
public class Invoker
{
private Command command;
public void SetCommand(Command command)
{
this.command = command;
}
public void ExecuteCommand()
{
command.Execute();
}
}
#endregion
#region 添加新的命令模式
public class NewCommand : Command
{
private NewReceiver newReceiver;
public NewCommand(NewReceiver newReceiver)
{
this.newReceiver = newReceiver;
}
public override void Execute()
{
newReceiver.Execute();
}
}
public class NewReceiver
{
public void Execute()
{
Console.WriteLine("new reveiver execute the newCommand...");
}
}
#endregion
#region 命令的请求的排队和延迟执行
public class CommandInvoker
{
private List<Command> commandQueue = new List<Command>();
public void AddCommand(Command command)
{
commandQueue.Add(command);
}
public void ExecuteCommands()
{
foreach (Command command in commandQueue)
{
command.Execute();
}
commandQueue.Clear();
}
public void DelayExecute(Command command,int delay)
{
Console.WriteLine($"等待开始....时间:{delay}ms");
new Thread(() =>
{
Console.WriteLine($"延时执行开始==>");
Thread.Sleep(delay);
command.Execute();
Console.WriteLine($"finish time:{Environment.NewLine}{DateTime.Now.ToString("HH:mm:ss fff")}");
Console.WriteLine($"==>延时执行完毕...");
}).Start();
}
}
#endregion
#region 命令撤销和重做操作
public interface ICommand
{
void Execute();
void Undo();
}
public class HistoryCommand : ICommand
{
private HistoryReceiver historyReceiver;
public HistoryCommand(HistoryReceiver historyReceiver)
{
this.historyReceiver = historyReceiver;
}
public void Execute()
{
historyReceiver.Execute();
}
public void Undo()
{
historyReceiver.UndoExecute();
}
}
public class HistoryReceiver
{
public void Execute()
{
Console.WriteLine("history receiver executes the command...");
}
public void UndoExecute()
{
Console.WriteLine("history receiver undoes the command...");
}
}
public class HistoryInvoker
{
private Stack<ICommand> commandStack = new Stack<ICommand>();
public void ExecuteCommand(ICommand command)
{
command.Execute();
commandStack.Push(command);
}
public void Undo()
{
if (commandStack.Count > 0)
{
ICommand command = commandStack.Pop();
Console.WriteLine("command Undo");
command.Undo();
}
else
{
Console.WriteLine("No commands to undo.");
}
}
public void Redo()
{
if (commandStack.Count>0)
{
ICommand command = commandStack.Peek();
Console.WriteLine("command Redo");
command.Execute();
}
else
{
Console.WriteLine("No commands to redo.");
}
}
}
internal class Program
{
static void Main(string[] args)
{
Receiver receiver = new Receiver();
Command sendCommand = new SendCommand(receiver);
Invoker invoker = new Invoker();
invoker.SetCommand(sendCommand);
invoker.ExecuteCommand();
Console.WriteLine("添加新命令------------------------------------");
NewReceiver newReceiver = new NewReceiver();
Command newCommand = new NewCommand(newReceiver);
invoker.SetCommand(newCommand);
invoker.ExecuteCommand();
Console.WriteLine("请求队列------------------------------------");
Receiver receiver1 = new Receiver();
Command command1 = new SendCommand(receiver1);
Command command2 = new SendCommand(receiver1);
CommandInvoker commandInvoker = new CommandInvoker();
commandInvoker.AddCommand(command1);
commandInvoker.AddCommand(command2);
commandInvoker.ExecuteCommands();
Console.WriteLine("延时执行------------------------------------");
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss fff")}");
commandInvoker.DelayExecute(command1,1000);
Console.WriteLine("准备撤销重做------------------------------------");
HistoryReceiver historyReceiver = new HistoryReceiver();
ICommand command3 = new HistoryCommand(historyReceiver);
ICommand command4 = new HistoryCommand(historyReceiver);
HistoryInvoker historyInvoker = new HistoryInvoker();
historyInvoker.ExecuteCommand(command3);
historyInvoker.ExecuteCommand(command4);
Console.WriteLine("执行撤销重做------------------------------------");
historyInvoker.Undo();
historyInvoker.Undo();
historyInvoker.Redo();
Console.WriteLine("END------------------------------------");
Console.ReadLine();
}
}
#endregion
运行结果
