一、定义
命令模式是一个高内聚的模式,其定义为:Encapsulate a request as an object,there by letting you parameterize clients with different requests,queue or log requests,and support undoable operations.(将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请 求排队或者记录请求日志,可以提供命令的撤销和恢复功能。)
通用类图:
在该类图中,我们看到三个角色:
Receiver接受者角色:该角色就是干活的角色,命令传递到这里是应该被执行的 Command命令角色:需要执行的所有命令都在这里声明 Invoker调用者角色:接收到命令,并执行命令
设计模式学习之命令模式实例讲解
分析:帅哥顾客,土豪订单,美女服务员,资深大厨的角色是什么?
帅哥顾客:Client
土豪订单:实现Command接口的具体Command
美女服务员:Invoker
资深大厨:Receiver
代码实现:
Command接口
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wolfy.命令模式
{
/// <summary>
/// Command为所有命令声明一个接口,调用命令对象的excute方法
/// 就可以让接收者进行相关的动作,
/// </summary>
public
abstract
class
Command
{
public
abstract
void
Execute();
}
}
?
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Wolfy.命令模式
{
/// <summary>
/// 具体的命令
/// </summary>
public
class
OrderCommand : Command
{
/// <summary>
///持有接受者对象
/// </summary>
SeniorChef receiver;
Order order;
public
OrderCommand(SeniorChef receiver, Order order)
{
this
.receiver = receiver;
this
.order = order;
}
public
override
void
Execute()
{
Console.WriteLine(
"{0}桌的订单:"
, order.DiningTable);
foreach (string item in order.FoodDic.Keys)
{
//通常会转调接收者对象的相应方法,让接收者来真正执行功能
receiver.MakeFood(order.FoodDic[item],item);
}
Thread.Sleep(
2000
);
//停顿一下 模拟做饭的过程
Console.WriteLine(
"{0}桌的饭弄好了"
, order.DiningTable);
}
}
}
Waitor:Invoker调用者,seniorChef:接收者 厨师类
?
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wolfy.命令模式
{
/// <summary>
/// 美女服务员类 这里作为调用者Invoker的角色
/// </summary>
public
class
Waitor
{
ArrayList commands =
null
;
//可以持有很多的命令对象
public
Waitor()
{
commands =
new
ArrayList();
}
public
void
SetCommand(Command cmd)
{
commands.Add(cmd);
}
/// <summary>
/// 提交订单 喊 订单来了,厨师开始执行
/// </summary>
public
void
OrderUp()
{
Console.WriteLine(
"美女服务员:叮咚,大厨,新订单来了......."
);
Console.WriteLine(
"资深厨师:收到"
);
for
(
int
i =
0
; i < commands.Count; i++)
{
Command cmd = commands[i] as Command;
if
(cmd !=
null
)
{
cmd.Execute();
}
}
}
}
}
Waitor
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wolfy.命令模式
{
/// <summary>
/// 资深大厨类 是命令的Receiver
/// </summary>
public
class
SeniorChef
{
public
void
MakeFood(
int
num,string foodName)
{
Console.WriteLine(
"{0}份{1}"
, num,foodName);
}
}
}
SeniorChef
订单Order,封装订单内容,然后传入OrderCommand,将订单对象变为命令对象
?
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wolfy.命令模式
{
/// <summary>
/// 订单
/// </summary>
public
class
Order
{
/// <summary>
/// 餐桌号码
/// </summary>
public
int
DiningTable { set; get; }
/// <summary>
/// food key:饭名 value:多少份
/// </summary>
public
Dictionary<string,
int
=
""
> FoodDic { set; get; }
}
}
Order</string,>
测试端Program相当于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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wolfy.命令模式
{
class
Program
{
static
void
Main(string[] args)
{
//program类 作为客户端
//创建2个order
Order order1 =
new
Order();
order1.DiningTable =
1
;
order1.FoodDic =
new
Dictionary<string,
int
=
""
>() ;
order1.FoodDic.Add(
"西红柿鸡蛋面"
,
1
);
order1.FoodDic.Add(
"小杯可乐"
,
2
);
Order order2 =
new
Order();
order2.DiningTable =
3
;
order2.FoodDic =
new
Dictionary<string,
int
=
""
>();
order2.FoodDic.Add(
"尖椒肉丝盖饭"
,
1
);
order2.FoodDic.Add(
"小杯雪碧"
,
1
);
//创建接收者
SeniorChef receiver=
new
SeniorChef();
//将订单这个两个消息封装成命令对象
OrderCommand cmd1 =
new
OrderCommand(receiver, order1);
OrderCommand cmd2 =
new
OrderCommand(receiver, order2);
//创建调用者 waitor
Waitor invoker =
new
Waitor();
//添加命令
invoker.SetCommand(cmd1);
invoker.SetCommand(cmd2);
//将订单带到柜台 并向厨师喊 订单来了
invoker.OrderUp();
Console.Read();
}
}
}</string,></string,>
测试结果:
命令模式优点:
1.降低对象之间的耦合度。
2.新的命令可以很容易地加入到系统 中。
3.可以比较容易地设计一个组合命令。
4.调用同一方法实现不同的功能
缺点:
使用命令模式可能会导致某些系统有过多的具体命令类。因为针对每一个命令都需要设计一个具体命令类,因此某些系统可能需要大量具体命令类,这将影响命令模式的使用。
适用环境:
1.系统需要将请求调用者和请求接收者解耦,使得调用者和接收者不直接交互。
2.系统需要在不同的时间指定请求、将请求排队和执行请求。
3.系统需要支持命令的撤销(Undo)操作和恢复(Redo)操作。
4.系统需要将一组操作组合在一起,即支持宏命令。