智能生活项目需求 看一个具体的需求 1) 我们买了一套智能家电,有照明灯、风扇、冰箱、洗衣机,我们只要在手机上安装app就 可以控制对这些家电工作。 2) 这些智能家电来自不同的厂家,我们不想针对每一种家电都安装一个App,分别控制,我 们希望只要一个app就可以控制全部智能家电。
3) 要实现一个app控制所有智能家电的需要,则每个智能家电厂家都要提供一个统一的接口 给app调用,这时 就可以考虑使用命令模式。

4) 命令模式可将“动作的请求者”从“动作的执行者”对象中解耦出来. 5) 在我们的例子中,动作的请求者是手机app,动作的执行者是每个厂商的一个家电产品命令模式基本介绍 基本介绍 1) 命令模式(Command Pattern):在软件设计中,我们经常需要 向某些对象发送请求,但是并不知道请求的接收者是谁,也不知 道被请求的操作是哪个, 我们只需在程序运行时指定具体的请求接收者即可,此时,可以 使用命令模式来进行设计 2) 命名模式使得请求发送者与请求接收者消除彼此之间的耦合,让 对象之间的调用关系更加灵活,实现解耦。 3) 在命名模式中,会将一个请求封装为一个对象,以便使用不同参 数来表示不同的请求,同时命令模式也支持可撤销的操作。 4) 通俗易懂的理解:将军发布命令,士兵去执行。
其中有几个角色: 将军(命令发布者)、士兵(命令的具体执行者)、命令。
Invoker是调用者(将军),Receiver是被调用者(士兵), MyCommand是命令,实现了Command接口,持有接收对象命令模式的原理类图3) 代码实现
namespace
command
{
//创建命令接口
interface
Command
{
//执行动作
execute
:
void
;
//撤销
undo
:
void
;
}
class
LightOffCommand
implements
Command
{
private
light
: LightReceiver
;
public
constructor
{
this
.
light
=
light
;
}
public
execute
:
void
{
// 调用接受者方法
this
.
light
.
off
;
}
public
undo
:
void
{
this
.
light
.
on
;
}
}
class
LightOnCommand
implements
Command
{
private
light
: LightReceiver
;
public
constructor
{
this
.
light
=
light
;
}
public
execute
:
void
{
// 调用接受者方法
this
.
light
.
on
;
}
public
undo
:
void
{
this
.
light
.
off
;
}
}
class
LightReceiver
{
public
on
:
void
{
console
.
log
;
}
public
off
:
void
{
console
.
log
;
}
}
//没有任何命令,即空执行:用于初始化每个按钮,当调用空命令时,对象什么都不做
//其实,这也是一种设计模式,可以省去空判断
class
NoCommand
implements
Command
{
public
execute
:
void
{
// TODO Auto-generated method stub
}
public
undo
:
void
{
// TODO Auto-generated method stub
}
}
//
class
RemoteController
{
//开按钮的命令数组
onCommands
: Array
<
Command
>;
offCommands
: Array
<
Command
>;
//执行撤销的命令
undoCommand
: Command
;
//构造器,完成对按钮的初始化
public
constructor
{
this
.
onCommands
=
new
Array
<
Command
>;
this
.
offCommands
=
new
Array
<
Command
>;
for
{
this
.
onCommands
[
i
]
=
new
NoCommand
;
this
.
offCommands
[
i
]
=
new
NoCommand
;
}
this
.
undoCommand
=
new
NoCommand
;
}
//给我们的按钮设置你需要的命令
public
setCommand
:
void
{
this
.
onCommands
[
no
]
=
onCommand
;
this
.
offCommands
[
no
]
=
offCommand
;
}
//按下开按钮
public
onButtonWanPushed
:
void
{
//找到你按下的按钮,并调用对应的方法
this
.
onCommands
[
no
].
execute
;
//记录这次的操作,用于操作
this
.
undoCommand
=
this
.
onCommands
[
no
];
}
//按下关按钮
public
offButtonWanPushed
:
void
{
//找到你按下的按钮,并调用对应的方法
this
.
offCommands
[
no
].
execute
;
//记录这次的操作,用于操作
this
.
undoCommand
=
this
.
offCommands
[
no
];
}
//撤销
public
undoButtonWasPushed
:
void
{
this
.
undoCommand
.
undo
;
}
}
class
TVOffCommand
implements
Command
{
private
light
: TVReceiver
;
public
constructor
{
this
.
light
=
light
;
}
public
execute
:
void
{
// 调用接受者方法
this
.
light
.
off
;
}
public
undo
:
void
{
this
.
light
.
on
;
}
}
class
TVOnCommand
implements
Command
{
private
light
: TVReceiver
;
public
constructor
{
this
.
light
=
light
;
}
public
execute
:
void
{
// 调用接受者方法
this
.
light
.
on
;
}
public
undo
:
void
{
this
.
light
.
off
;
}
}
class
TVReceiver
{
public
on
:
void
{
console
.
log
;
}
public
off
:
void
{
console
.
log
;
}
}
class
Client
{
public
constructor
{
//使用命令设计模式,完成遥控器,对电灯的操作
//创建电灯的对象(接受者)
let
lightReceiver
=
new
LightReceiver
;
//创建电灯相关的开关命令
let
lightOnCommand
=
new
LightOnCommand
;
let
lightOffCommand
=
new
LightOffCommand
;
//创建电视的对象(接受者)
let
tVReceiver
=
new
TVReceiver
;
//创建电视相关的开关命令
let
tVOnCommand
=
new
TVOnCommand
;
let
tVOffCommand
=
new
TVOffCommand
;
//需要一个遥控器
let
remoteController
=
new
RemoteController
;
//给遥控器设置命令
remoteController
.
setCommand
;
remoteController
.
setCommand
;
console
.
log
;
remoteController
.
onButtonWanPushed
;
console
.
log
;
remoteController
.
offButtonWanPushed
;
console
.
log
;
remoteController
.
undoButtonWasPushed
;
console
.
log
;
remoteController
.
onButtonWanPushed
;
console
.
log
;
remoteController
.
offButtonWanPushed
;
console
.
log
;
remoteController
.
undoButtonWasPushed
;
}
}
new
Client
;
}
测试:命令模式的注意事项和细节 1) 将发起请求的对象与执行请求的对象解耦。发起请求的对象是调用者,调用者只要 调用命令对象的execute方法就可以让接收者工作,而不必知道具体的接收者对 象是谁、是如何实现的,命令对象会负责让接收者执行请求的动作,也就是说:” 请求发起者”和“请求执行者”之间的解耦是通过命令对象实现的,命令对象起到 了纽带桥梁的作用。 2) 容易设计一个命令队列。只要把命令对象放到列队,就可以多线程的执行命令 3) 容易实现对请求的撤销和重做 4) 命令模式不足:可能导致某些系统有过多的具体命令类,增加了系统的复杂度,这 点在在使用的时候要注意 5) 空命令也是一种设计模式,它为我们省去了判空的操作。在上面的实例中,如果没 有用空命令,我们每按下一个按键都要判空,这给我们编码带来一定的麻烦。