1、定义:
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。[GOF 《设计模式》]
2、角色
Command——用来执行操作的接口。
CreateCommand——将一个接收者对象绑定一个动作,并实现执行命令操作。
Invoker——要求该命令执行。
Receiver——知道如何实施与执行一个与请求相关的操作,任何类都可作为一个接收者。
3、模型
Command
- abstract class Command
- {
- protected Receiver receiver;
- public Command(Receiver creceiver)
- {
- this.receiver = creceiver;
- }
- abstract public void Execute();
- }
CreateCommand
- class CreateCommand : Command
- {
- public CreateCommand(Receiver receiver)
- : base(receiver)
- {
- }
- public override void Execute()
- {
- receiver.Action();
- }
- }
Receiver
- class Receiver
- {
- public void Action()
- {
- Console.WriteLine("执行命令");
- }
- }
Invoker
- class Invoker
- {
- private Command command;
- public void SetCommand(Command command)
- {
- this.command = command;
- }
- public void ExecuteCommand()
- {
- command.Execute();
- }
- }
调用
- static void Main(string[] args)
- {
- Receiver receiver = new Receiver();
- Command command = new CreateCommand(receiver);
- Invoker invoker = new Invoker();
- invoker.SetCommand(command);
- invoker.ExecuteCommand();
- Console.ReadLine();
- }
结果:
命令模式模型代码:http://download.youkuaiyun.com/detail/yysyangyangyangshan/4089536
命令模式,很多个Receiver,并与之对应Command也有很多个,Invoker则负责将Receiver和Command关联,并执行。
应用模型:
- abstract class Command1
- {
- protected Receiver1 receiver1;
- public Command1(Receiver1 creceiver1)
- {
- this.receiver1 = creceiver1;
- }
- abstract public void Execute();
- }
- abstract class Command2
- {
- protected Receiver2 receiver2;
- public Command2(Receiver2 creceiver2)
- {
- this.receiver2 = creceiver2;
- }
- abstract public void Execute();
- }
- class CreateCommand1 : Command1
- {
- public CreateCommand1(Receiver1 receiver1)
- : base(receiver1)
- {
- }
- public override void Execute()
- {
- receiver1.Action();
- }
- }
- class CreateCommand2 : Command2
- {
- public CreateCommand2(Receiver2 receiver2)
- : base(receiver2)
- {
- }
- public override void Execute()
- {
- receiver2.Action();
- }
- }
- class Invoker
- {
- private Command1 command1;
- private Command2 command2;
- public void SetCommand1(Command1 command)
- {
- this.command1 = command;
- }
- public void ExecuteCommand1()
- {
- command1.Execute();
- }
- public void SetCommand2(Command2 command)
- {
- this.command2 = command;
- }
- public void ExecuteCommand2()
- {
- command2.Execute();
- }
- }
- class Receiver1
- {
- public void Action()
- {
- Console.WriteLine("执行命令一");
- }
- }
- class Receiver2
- {
- public void Action()
- {
- Console.WriteLine("执行命令二");
- }
- }
调用
- static void Main(string[] args)
- {
- Receiver1 receiver1 = new Receiver1();
- Receiver2 receiver2 = new Receiver2();
- Command1 command1 = new CreateCommand1(receiver1);
- Command2 command2 = new CreateCommand2(receiver2);
- Invoker invoker = new Invoker();
- invoker.SetCommand1(command1);
- invoker.ExecuteCommand1();
- invoker.SetCommand2(command2);
- invoker.ExecuteCommand2();
- Console.ReadLine();
- }
结果:
应用代码:http://download.youkuaiyun.com/detail/yysyangyangyangshan/4089547