设计模式学习笔记--命令模式

 1 using System;
 2 
 3 namespace Command
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/31 20:21:09 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Receiver说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class Receiver
12     {
13         public void Action()
14         {
15             Console.WriteLine("执行请求!");
16         }
17     }
18 }
View Code
 1 using System;
 2 
 3 namespace Command
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/31 20:20:44 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Command说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public abstract class Command
12     {
13         protected Receiver receiver;
14 
15         public Command(Receiver receiver)
16         {
17             this.receiver = receiver;
18         }
19 
20         public abstract void Execute();
21     }
22 }
View Code
 1 using System;
 2 
 3 namespace Command
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/31 20:23:11 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// ConcreteCommand说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class ConcreteCommand:Command
12     {
13         public ConcreteCommand(Receiver receiver)
14             : base(receiver)
15         { }
16 
17         public override void Execute()
18         {
19             receiver.Action();
20         }
21     }
22 }
View Code
 1 using System;
 2 
 3 namespace Command
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/31 20:24:58 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Invoker说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class Invoker
12     {
13         private Command command;
14 
15         public void SetCommand(Command command)
16         {
17             this.command = command;
18         }
19 
20        public void ExecuteCommand()
21        {
22            command.Execute();
23        }
24     }
25 }
View Code
 1 namespace Command
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Receiver receiver = new Receiver();
 8             Command command = new ConcreteCommand(receiver);
 9             Invoker invoker = new Invoker();
10 
11             invoker.SetCommand(command);
12             invoker.ExecuteCommand();
13         }
14     }
15 }
View Code
原文地址:https://www.cnblogs.com/bzyzhang/p/5547383.html