设计模式 命令模式(Command Pattern)

意图

  将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。

适用性  

  1.抽象出待执行的动作以参数化某对象,你可用过程语言中的回调(c a l l b a c k )函数表达这种参数化机制。所谓回调函数是指函数先在某处注册,而它将在稍后某个需要的时候被调用。C o m m a n d 模式是回调机制的一个面向对象的替代品。  

  2.在不同的时刻指定、排列和执行请求。一个C o m m a n d 对象可以有一个与初始请求无关的生存期。如果一个请求的接收者可用一种与地址空间无关的方式表达,那么就可将负责该请求的命令对象传送给另一个不同的进程并在那儿实现该请求。  

  3.在不同的时刻指定、排列和执行请求。一个C o m m a n d 对象可以有一个与初始请求无关的生存期。如果一个请求的接收者可用一种与地址空间无关的方式表达,那么就可将负责该请求的命令对象传送给另一个不同的进程并在那儿实现该请求。  

  4.支持修改日志,这样当系统崩溃时,这些修改可以被重做一遍。在C o m m a n d 接口中添加装载操作和存储操作,可以用来保持变动的一个一致的修改日志。从崩溃中恢复的过程包括从磁盘中重新读入记录下来的命令并用E x e c u t e 操作重新执行它们。 

  5.用构建在原语操作上的高层操作构造一个系统。这样一种结构在支持事务( 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

 1 // Command
2
3 /* Notes:
4 * Commands are at the heart of a modern GUI app.
5 * They must be nameable, undoable, recordable,
6 * configurable, executable and repeatable.
7 *
8 * In addition to the command here, a command type could also be useful.
9 * It could store the name of a command, information about its icon, etc.
10 */
11
12 namespace Command_DesignPattern
13 {
14 using System;
15
16 abstract class Command
17 {
18 abstract public void Execute();
19 protected Receiver r;
20 public Receiver R
21 {
22 set
23 {
24 r = value;
25 }
26 }
27 }
28
29 class ConcreteCommand : Command
30 {
31 override public void Execute()
32 {
33 Console.WriteLine("Command executed");
34 r.InformAboutCommand();
35 }
36 }
37
38 class Receiver
39 {
40 public void InformAboutCommand()
41 {
42 Console.WriteLine("Receiver informed about command");
43 }
44
45 }
46
47 class Invoker
48 {
49 private Command command;
50 public void StoreCommand(Command c)
51 {
52 command = c;
53 }
54 public void ExecuteCommand()
55 {
56 command.Execute();
57 }
58 }
59
60 /// <summary>
61 /// Summary description for Client.
62 /// </summary>
63 public class Client
64 {
65 public static int Main(string[] args)
66 {
67 // Set up everything
68 Command c = new ConcreteCommand();
69 Receiver r = new Receiver();
70 c.R = r;
71 Invoker i = new Invoker();
72 i.StoreCommand(c);
73
74 // now let application run
75
76 // the invoker is how the command is exposed for the end-user
77 // (or a client) initiates the command,
78 // (e.g. toolbar button, menu item)
79
80 i.ExecuteCommand();
81
82 return 0;
83 }
84 }
85 }



人生如棋、我愿为卒、行动虽缓、从未退过

原文地址:https://www.cnblogs.com/sunjinpeng/p/2437756.html