???????命令模式(Command Pattern)是对象的行为模式,其意图是将请求封装在对象的内部。
?命令模式涉及的角色有:
???示例:
?????????在生活只能电视机就是一个很好的命令模式的实例。?遥控器就是一个Invoker,?电视机就是一个Receiver;使用遥控器的人就是一个Client;遥控器上的按钮就是一个Concrete Command.
????????假设一个电视机?有开,关,切换频道的基本功能。有一个遥控器上,有开,关按钮,还有频道1,频道2,频道3?的按钮。?首先需要将遥控器和电视机进行匹配,匹配后就可以通过遥控器控制电视机。
?
class="java">/**
*电视机的抽象接口,定义了一个电视机的基本操作
* @author zhangwei_david
* @version $Id: TV.java, v 0.1 2014年11月21日 下午4:34:05 zhangwei_david Exp $
*/
public interface TV {
/**
* 打开电视机
*/
public void open();
/**
* 关闭电视机
*/
public void close();
/**
* 切换频道
*
* @param channel
*/
public void change(int channel);
}
?
/**
*具体的电视机
* @author zhangwei_david
* @version $Id: SonyTV.java, v 0.1 2014年11月21日 下午4:35:05 zhangwei_david Exp $
*/
public class SonyTV implements TV {
private enum State {
OPEN, CLOSE;
}
private State state = State.CLOSE;
/**
* @see com.cathy.demo.pattern.command.TV#open()
*/
public void open() {
if (state == State.CLOSE) {
state = State.OPEN;
System.out.println("打开电视机");
}
}
/**
* @see com.cathy.demo.pattern.command.TV#close()
*/
public void close() {
if (state == State.OPEN) {
state = State.CLOSE;
System.out.println("关闭电视机");
}
}
/**
* @see com.cathy.demo.pattern.command.TV#change(int)
*/
public void change(int channel) {
if (state == State.OPEN) {
System.out.println("切换到 " + channel + " 频道");
}
}
}
?
/**
*抽象的命令接口
* @author zhangwei_david
* @version $Id: Command.java, v 0.1 2014年11月21日 下午4:27:15 zhangwei_david Exp $
*/
public interface Command {
/**
* 执行命令
*/
public void execute();
}
?
/**
*具体的命令-打开
* @author zhangwei_david
* @version $Id: Open.java, v 0.1 2014年11月21日 下午4:28:37 zhangwei_david Exp $
*/
public class Open implements Command {
/**接收者**/
private TV tv;
public Open(TV tv) {
super();
this.tv = tv;
}
/**
* @see com.cathy.demo.pattern.command.Command#execute()
*/
public void execute() {
tv.open();
}
}
?
/**
*具体的命令实现-关闭
* @author zhangwei_david
* @version $Id: Close.java, v 0.1 2014年11月21日 下午4:29:41 zhangwei_david Exp $
*/
public class Close implements Command {
private TV tv;
/**
* @see com.cathy.demo.pattern.command.Command#execute()
*/
public void execute() {
tv.close();
}
public Close(TV tv) {
super();
this.tv = tv;
}
}
?
/**
*
* @author zhangwei_david
* @version $Id: Change.java, v 0.1 2014年11月21日 下午4:31:13 zhangwei_david Exp $
*/
public class Change implements Command {
/**频道**/
private int channel;
/**
* Getter method for property <tt>channel</tt>.
*
* @return property value of channel
*/
public int getChannel() {
return channel;
}
/**
* Setter method for property <tt>channel</tt>.
*
* @param channel value to be assigned to property channel
*/
public void setChannel(int channel) {
this.channel = channel;
}
/**
* @see com.cathy.demo.pattern.command.Command#execute()
*/
public void execute() {
System.out.println("切换到 " + channel + " 频道");
}
public Change(int channel, TV tv) {
super();
this.channel = channel;
}
}
?
/** *请求者 -遥控器 * @author zhangwei_david * @version $Id: Telecontroller.java, v 0.1 2014年11月21日 下午4:39:00 zhangwei_david Exp $ */ public class Telecontroller { private Command open; private Command close; private Command one; private Command two; private Command three; public Telecontroller(TV tv) { open = new Open(tv); close = new Close(tv); one = new Change(1, tv); two = new Change(2, tv); three = new Change(3, tv); } /** * 打开电视机 */ public void open() { open.execute(); } /** * 关闭电视机 */ public void close() { close.execute(); } /** * 切换到频道1 */ public void one() { one.execute(); } /** * 切换到频道2 */ public void two() { two.execute(); } /** * 切换到频道3 */ public void three() { three.execute(); } }
?
/**
*
* @author zhangwei_david
* @version $Id: Client.java, v 0.1 2014年11月21日 下午4:40:19 zhangwei_david Exp $
*/
public class Client {
public static void main(String[] args) {
// 具体电视机
SonyTV sonyTV = new SonyTV();
// 遥控器和电视机进行配对
Telecontroller telecontroller = new Telecontroller(sonyTV);
// 通过遥控打开电视机
telecontroller.open();
// 通过遥控切换到1频道
telecontroller.one();
// 通过遥控切换到3频道
telecontroller.three();
// 通过遥控关闭电视机
telecontroller.close();
}
}
?
打开电视机 切换到 1 频道 切换到 3 频道 关闭电视机
?
?
?