首页 > 其他 > 详细

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

时间:2016-05-31 22:17:53      阅读:241      评论:0      收藏:0      [点我收藏+]
技术分享
 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

 

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

原文:http://www.cnblogs.com/bzyzhang/p/5547383.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!