设计模式——命令模式

名称Command
结构 Command.gif
意图将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。
适用性
  • 抽象出待执行的动作以参数化某对象,你可用过程语言中的回调(c a l l b a c k )函数表达这种参数化机制。所谓回调函数是指函数先在某处注册,而它将在稍后某个需要的时候被调用。C o m m a n d 模式是回调机制的一个面向对象的替代品。
  • 在不同的时刻指定、排列和执行请求。一个C o m m a n d 对象可以有一个与初始请求无关的生存期。如果一个请求的接收者可用一种与地址空间无关的方式表达,那么就可将负责该请求的命令对象传送给另一个不同的进程并在那儿实现该请求。
  • 支持取消操作。C o m m a n d 的E x c u t e 操作可在实施操作前将状态存储起来,在取消操作时这个状态用来消除该操作的影响。C o m m a n d 接口必须添加一个U n e x e c u t e 操作,该操作取消上一次E x e c u t e 调用的效果。执行的命令被存储在一个历史列表中。可通过向后和向前遍历这一列表并分别调用U n e x e c u t e 和E x e c u t e 来实现重数不限的“取消”和“重做”。
  • 支持修改日志,这样当系统崩溃时,这些修改可以被重做一遍。在C o m m a n d 接口中添加装载操作和存储操作,可以用来保持变动的一个一致的修改日志。从崩溃中恢复的过程包括从磁盘中重新读入记录下来的命令并用E x e c u t e 操作重新执行它们。
  • 用构建在原语操作上的高层操作构造一个系统。这样一种结构在支持事务( t r a n s a c t i o n )的信息系统中很常见。一个事务封装了对数据的一组变动。C o m m a n d 模式提供了对事务进行建模的方法。C o m m a n d 有一个公共的接口,使得你可以用同一种方式调用所有的事务。同时使用该模式也易于添加新事务以扩展系统。
Code Example
 1 None.gif //  Command
 2 None.gif
 3 None.gif //  Intent: "Encapsulate a request as an object, thereby letting you 
 4 None.gif //  parameterize clients with different requests, queue or log 
 5 None.gif //  requests, and support undoable operations". 
 6 None.gif
 7 None.gif //  For further information, read "Design Patterns", p233, Gamma et al.,
 8 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
 9 None.gif
10 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
11InBlock.gif * Commands are at the heart of a modern GUI app. 
12InBlock.gif * They must be nameable, undoable, recordable, 
13InBlock.gif * configurable, executable and repeatable. 
14InBlock.gif * 
15InBlock.gif * In addition to the command here, a command type could also be useful.
16InBlock.gif * It could store the name of a command, information about its icon, etc.  
17ExpandedBlockEnd.gif */

18 None.gif 
19 None.gif namespace  Command_DesignPattern
20 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
21InBlock.gif    using System;
22InBlock.gif
23InBlock.gif    abstract class Command 
24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
25InBlock.gif        abstract public void Execute();
26InBlock.gif        protected Receiver r;
27InBlock.gif        public Receiver R
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            set 
30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
31InBlock.gif                r = value; 
32ExpandedSubBlockEnd.gif            }

33ExpandedSubBlockEnd.gif        }

34ExpandedSubBlockEnd.gif    }

35InBlock.gif
36InBlock.gif    class ConcreteCommand : Command
37ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
38InBlock.gif        override public void Execute()
39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
40InBlock.gif            Console.WriteLine("Command executed");
41InBlock.gif            r.InformAboutCommand();
42ExpandedSubBlockEnd.gif        }

43ExpandedSubBlockEnd.gif    }

44InBlock.gif
45InBlock.gif    class Receiver 
46ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
47InBlock.gif        public void InformAboutCommand()
48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
49InBlock.gif            Console.WriteLine("Receiver informed about command");
50ExpandedSubBlockEnd.gif        }

51InBlock.gif        
52ExpandedSubBlockEnd.gif    }

53InBlock.gif
54InBlock.gif    class Invoker 
55ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
56InBlock.gif        private Command command;
57InBlock.gif        public void StoreCommand(Command c)
58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
59InBlock.gif            command = c;
60ExpandedSubBlockEnd.gif        }

61InBlock.gif        public void ExecuteCommand()
62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
63InBlock.gif            command.Execute();
64ExpandedSubBlockEnd.gif        }
        
65ExpandedSubBlockEnd.gif    }

66InBlock.gif
67ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
68InBlock.gif    ///    Summary description for Client.
69ExpandedSubBlockEnd.gif    /// </summary>

70InBlock.gif    public class Client
71ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
72InBlock.gif        public static int Main(string[] args)
73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{        
74InBlock.gif            // Set up everything
75InBlock.gif            Command c = new ConcreteCommand();
76InBlock.gif            Receiver r = new Receiver();
77InBlock.gif            c.R = r;
78InBlock.gif            Invoker i = new Invoker();
79InBlock.gif            i.StoreCommand(c);
80InBlock.gif
81InBlock.gif            // now let application run
82InBlock.gif
83InBlock.gif            // the invoker is how the command is exposed for the end-user 
84InBlock.gif            // (or a client) initiates the command, 
85InBlock.gif            // (e.g. toolbar button, menu item)
86InBlock.gif
87InBlock.gif            i.ExecuteCommand();
88InBlock.gif
89InBlock.gif            return 0;
90ExpandedSubBlockEnd.gif        }

91ExpandedSubBlockEnd.gif    }

92ExpandedBlockEnd.gif}

93 None.gif
94 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/08/05/207877.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值