设计模式之命令模式

释放双眼,带上耳机,听听看~!

1.命令模式的意图是什么?

        命令模式的意图是将请求封装在对象内部。从而可像操作对象那样来操作请求,使你可用不同的请求对客户进行参数化; 对请求排队或记录请求日志,以及支持可撤销的操作。

2.为什么用命令模式?

        有时我们无法控制方法执行的时机与上下文,此时,可将方法封装在对象的内部,通过在对象内部存储调用方法所需要的信息,就可让客户端或者服务决定何时调用该方法。

        在软件系统中,行为请求者与行为实现者通常是一种紧耦合的关系,但某些场合,比如需要对行为进行记录、撤销或重做、事务等处理时,这种无法抵御变化的紧耦合的设计就不太合适。

3.命令模式解读

  UML

设计模式之命令模式

 角色解读

1 )抽象命令(Command):定义命令的接口,声明执行的方法。
2)具体命令(ConcreteCommand):具体命令,实现要执行的方法,它通常是“虚”的实现;通常会有接收者,并调用接收者的功能来完成命令要执行的操作。
3)接收者(Receiver):真正执行命令的对象。任何类都可能成为一个接收者,只要能实现命令要求实现的相应功能。
4)调用者(Invoker):要求命令对象执行请求,通常会持有命令对象,可以持有很多的命令对象。这个是客户端真正触发命令并要求命令执行相应操作的地方,也就是说相当于使用命令对象的入口。
5) 客户端(Client):命令由客户端来创建,并设置命令的接收者。

 代码实现:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
1/// <summary>
2    /// 接收者类,知道如何实施与执行一个请求相关的操作,任何类都可能作为一个接收者。
3    /// </summary>
4    public class Receiver
5    {
6        /// <summary>
7        /// 真正的命令实现
8        /// </summary>
9        public void Action()
10        {
11            Console.WriteLine("Execute request!");
12        }
13    }
14
15    /// <summary>
16    /// 抽象命令类,用来声明执行操作的接口
17    /// </summary>
18    public interface ICommand
19    {
20        void Execute();
21    }
22
23    /// <summary>
24    /// 具体命令类,实现具体命令。
25    /// </summary>
26    public class ConcereteCommand : ICommand
27    {
28        // 具体命令类包含有一个接收者,将这个接收者对象绑定于一个动作
29        private Receiver receiver;
30
31        public ConcereteCommand(Receiver receiver)
32        {
33            this.receiver = receiver;
34        }
35
36        /// <summary>
37        /// 说这个实现是“虚”的,因为它是通过调用接收者相应的操作来实现Execute的
38        /// </summary>
39        public void Execute()
40        {
41            receiver.Action();
42        }
43    }
44
45    /// <summary>
46    /// 调度类,要求该命令执行这个请求
47    /// </summary>
48    public class Invoker
49    {
50        private ICommand command;
51
52        /// <summary>
53        /// 设置命令
54        /// </summary>
55        /// <param name="command"></param>
56        public void SetCommand(ICommand command)
57        {
58            this.command = command;
59        }
60
61        /// <summary>
62        /// 执行命令
63        /// </summary>
64        public void ExecuteCommand()
65        {
66            command.Execute();
67        }
68    }
69
70//客户端调用代码
71class Program
72    {
73        static void Main(string[] args)
74        {
75            Receiver receiver = new Receiver();
76            ICommand command = new ConcereteCommand(receiver);
77            Invoker invoker = new Invoker();
78
79            invoker.SetCommand(command);
80            invoker.ExecuteCommand();
81
82            Console.Read();
83        }
84    }
85

执行结果

设计模式之命令模式
4模式分析

 1)本质:对命令进行封装,将发出命令与执行命令的责任分开。
2) 每一个命令都是一个操作:请求的一方发出请求,要求执行一个操作;接收的一方收到请求,并执行操作。

 3)请求方和接收方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被执行、何时被执行,以及是怎么被执行的。

 4)使请求本身成为一个对象,这个对象和其它对象一样可以被存储和传递。

 5) 命令模式的关键在于引入了抽象命令接口,且发送者针对抽象命令接口编程,只有实现了抽象命令接口的具体命令才能与接收者相关联。 
5. 模式总结

   优点

    1) 解除了请求者与实现者之间的耦合,降低了系统的耦合度。

    2) 对请求排队或记录请求日志,支持撤销操作。

    3) 可以容易地设计一个组合命令。

    4)新命令可以容易地加入到系统中。

  缺点

    因为针对每一个命令都需要设计一个具体命令类,使用命令模式可能会导致系统有过多的具体命令类。
5.适用场景

 1)当需要对行为进行“记录、撤销/重做”等处理时。

 2)系统需要将请求者和接收者解耦,使得调用者和接收者不直接交互。

 3)系统需要在不同时间指定请求、请求排队和执行请求。

 4)系统需要将一组操作组合在一起,即支持宏命令。

 5)典型应用场合:菜单命令

        支持菜单的工具包通常都使用了命令模式,每个菜单项关联一个对象,以便在用户单击该菜单项时,可以执行相关的行为。当用户单击菜单时,该如何让类调用相关的方法呢?答案是使用多态。

6.举例:银行帐号的存款、提款

 1)UML

 2)代码实现


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
1/// <summary>
2/// 银行帐号类,充当接收者角色
3/// </summary>
4public class Account
5{
6/// <summary>
7/// 帐号总金额
8/// </summary>
9private decimal totalAmount { get; set; }
10
11/// <summary>
12/// 存钱
13/// </summary>
14/// <param name="amount"></param>
15public void MoneyIn(decimal amount)
16{
17this.totalAmount += amount;
18}
19
20/// <summary>
21/// 取钱
22/// </summary>
23/// <param name="amount"></param>
24public void MoneyOut(decimal amount)
25{
26this.totalAmount -= amount;
27}
28
29public decimal GetTotalAmout()
30{
31return totalAmount;
32}
33}
34
35public abstract class Command
36{
37protected Account account;
38
39public Command(Account account)
40{
41this.account = account;
42}
43
44public abstract void Execute();
45}
46
47/// <summary>
48/// 存款命令
49/// </summary>
50public class MoneyInCommand : Command
51{
52private decimal amount;
53
54public MoneyInCommand(Account account, decimal amount)
55: base(account)
56{
57this.amount = amount;
58}
59
60/// <summary>
61/// 实现存钱命令
62/// </summary>
63public override void Execute()
64{
65account.MoneyIn(amount);
66}
67}
68
69/// <summary>
70/// 取款命令类
71/// </summary>
72public class MoneyOutCommand : Command
73{
74private decimal amount;
75public MoneyOutCommand(Account account, decimal amount)
76: base(account)
77{
78this.amount = amount;
79}
80
81/// <summary>
82/// 实现取钱命令
83/// </summary>
84public override void Execute()
85{
86account.MoneyOut(amount);
87}
88}
89
90public class Invoker
91{
92private Command command;
93
94public void SetCommand(Command command)
95{
96this.command = command;
97}
98
99public void ExecuteCommand()
100{
101command.Execute();
102}
103}
104
105
106//客户端代码
107class Program
108{
109static void Main(string[] args)
110{
111// 创建银行帐号
112Account account = new Account();
113// 创建一个存入500元的命令
114Command commandIn = new MoneyInCommand(account,500);
115// 创建一个调度者
116BankAccount.Invoker invoker = new BankAccount.Invoker();
117
118// 设置存钱命令
119invoker.SetCommand(commandIn);
120// 执行
121invoker.ExecuteCommand();
122Console.WriteLine("The current amount is " + account.GetTotalAmout().ToString("N2"));
123
124// 再次存入500
125Command commandIn2 = new MoneyInCommand(account, 500);
126invoker.SetCommand(commandIn2);
127invoker.ExecuteCommand();
128Console.WriteLine("The current amount is " + account.GetTotalAmout().ToString("N2"));
129
130// 取出300
131Command commandOut = new MoneyOutCommand(account, 300);
132invoker.SetCommand(commandOut);
133invoker.ExecuteCommand();
134Console.WriteLine("The current amount is " + account.GetTotalAmout().ToString("N2"));
135
136Console.Read();
137}
138}
139

执行结果

设计模式之命令模式

给TA打赏
共{{data.count}}人
人已打赏
安全运维

MongoDB最简单的入门教程之二 使用nodejs访问MongoDB

2021-12-11 11:36:11

安全运维

Ubuntu上NFS的安装配置

2021-12-19 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索