1、定义:

  将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象,命令模式也支持可撤销的操作。
命令可以用来实现日志和事务系统。

2、实例:

1)需求:设计一个家电遥控器的API,遥控器具有7个可编程的插槽,每个插槽都具有对应的开关按钮,另外还具备撤销按钮,用来撤销上一步的操作。

2)代码实现:

  a)命令实现

/* 遥控器命令接口,实现遥控器的动作 */
public interface Command {
public void execute();
public void undo();
} /* 空对象,用于返回一个没有意义的对象*/
public class NoCommand implements Command{
@Override
public void execute() { } @Override
public void undo() { }
}

  b) 命令对象

public class Light {
private String name; public Light(String name) {
this.name = name;
} public Light() {
} public void lightOn(){
System.out.println(name + " light is on!");
} public void lightOff(){
System.out.println(name + " light is off!");
}
} public class LightOnCommand implements Command {
Light light = new Light();
public LightOnCommand(Light light) {
this.light = light;
} @Override
public void execute() {
light.lightOn();
} @Override
public void undo() {
light.lightOff();
}
} public class LightOffCommand implements Command {
Light light = new Light(); public LightOffCommand(Light light) {
this.light = light;
} @Override
public void execute() {
light.lightOff();
} @Override
public void undo() {
light.lightOn();
}
}

  c) 命令对象管理

public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
Command undoCommand; public RemoteControl() {
onCommands = new Command[7];
offCommands = new Command[7];
Command noCommand = new NoCommand();
for(int i=0;i<7;i++){
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
undoCommand = noCommand;
} public void setCommand(int slot,Command onCommand,Command offCommand){
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
} public void onButtonWasPushed(int slot){
onCommands[slot].execute();
undoCommand = onCommands[slot];
} public void offButtonWasPushed(int slot){
offCommands[slot].execute();
undoCommand = offCommands[slot];
} public void undoButtonWasPushed(){
undoCommand.undo();
} @Override
public String toString() {
StringBuffer strBuff = new StringBuffer();
strBuff.append("\n===========Remote Control=============\n");
for(int i=0;i<onCommands.length;i++){
strBuff.append("[slot "+i+"]"+onCommands[i].getClass().getName()+" "
+offCommands[i].getClass().getName()+"\n");
}
return strBuff.toString();
}
}

  d) 测试代码:

public class RemoteLoader {
public static void main(String[] args) {
RemoteControl remoteControl = new RemoteControl(); Light livingRoomLight = new Light("living Room");
Light kitchenLight = new Light("kitchen");
Light washingRoomLight = new Light("washing Room"); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);
LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight); LightOnCommand washingRoomLightOn = new LightOnCommand(washingRoomLight);
LightOffCommand washingRoomLightOff = new LightOffCommand(washingRoomLight); remoteControl.setCommand(0,livingRoomLightOn,livingRoomLightOff);
remoteControl.setCommand(1,kitchenLightOn,kitchenLightOff);
remoteControl.setCommand(2,washingRoomLightOn,washingRoomLightOff); System.out.println(remoteControl); remoteControl.onButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(0);
remoteControl.offButtonWasPushed(1);
remoteControl.offButtonWasPushed(2);
remoteControl.undoButtonWasPushed();
}
}

  

测试结果:
===========Remote Control=============
[slot 0]com.zte.common.utils.DesignPattern.CommandPattern.LightOnCommand com.zte.common.utils.DesignPattern.CommandPattern.LightOffCommand
[slot 1]com.zte.common.utils.DesignPattern.CommandPattern.LightOnCommand com.zte.common.utils.DesignPattern.CommandPattern.LightOffCommand
[slot 2]com.zte.common.utils.DesignPattern.CommandPattern.LightOnCommand com.zte.common.utils.DesignPattern.CommandPattern.LightOffCommand
[slot 3]com.zte.common.utils.DesignPattern.CommandPattern.NoCommand com.zte.common.utils.DesignPattern.CommandPattern.NoCommand
[slot 4]com.zte.common.utils.DesignPattern.CommandPattern.NoCommand com.zte.common.utils.DesignPattern.CommandPattern.NoCommand
[slot 5]com.zte.common.utils.DesignPattern.CommandPattern.NoCommand com.zte.common.utils.DesignPattern.CommandPattern.NoCommand
[slot 6]com.zte.common.utils.DesignPattern.CommandPattern.NoCommand com.zte.common.utils.DesignPattern.CommandPattern.NoCommand

living Room light is on!
kitchen light is on!
washing Room light is on!
living Room light is off!
kitchen light is off!
washing Room light is off!
washing Room light is on!

JAVA设计模式---命令模式的更多相关文章

  1. 【设计模式】Java设计模式 - 命令模式

    Java设计模式 - 命令模式 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 目录 Ja ...

  2. JAVA 设计模式 命令模式

    用途 命令模式 (Command) 将一个请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化:对请求排队或请求日志,以及支持可撤销的操作. 命令模式是一种行为型模式. 结构

  3. Java设计模式-命令模式(Command)

    命令模式很好理解,举个例子,司令员下令让士兵去干件事情,从整个事情的角度来考虑,司令员的作用是,发出口令,口令经过传递,传到了士兵耳朵里,士兵去执行.这个过程好在,三者相互解耦,任何一方都不用去依赖其 ...

  4. Java设计模式—命令模式

    命令模式是一个高内聚的模式. 定义如下:将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能. 通用类图如下: 角色说明: ● Re ...

  5. Java设计模式の命令模式

    意图: 将一个请求封装为一个对象,从而可用不同的请求对客户进行参数化:对请求排队或记录日志,以及支持可撤销的操作 动机: 将”发出请求的对象”和”接收与执行这些请求的对象”分隔开来. 效果: 1).c ...

  6. linkin大话设计模式--命令模式

    linkin大话设计模式--命令模式 首先考虑一种应用情况,某个方法需要完成某一个功能,这个功能的大部分功能已经确定了,但是有可能少量的步骤没法确定,必须等到执行这个方法才可以确定. 也就是说,我们写 ...

  7. Java设计模式——组合模式

    JAVA 设计模式 组合模式 用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模 ...

  8. java设计模式--单列模式

    java设计模式--单列模式 单列模式定义:确保一个类只有一个实例,并提供一个全局访问点. 下面是几种实现单列模式的Demo,每个Demo都有自己的优缺点: Demo1: /** * 单列模式需要满足 ...

  9. 3.java设计模式-建造者模式

    Java设计模式-建造者模式 在<JAVA与模式>一书中开头是这样描述建造(Builder)模式的: 建造模式是对象的创建模式.建造模式可以将一个产品的内部表象(internal repr ...

随机推荐

  1. 集合 (set) 的增删改查及 copy()方法

    一.集合 1.集合的创建 set1 = set({1,2,'barry'}) set2 = {1,2,'barry'} print(set1,type(set1)) print(set2,type(s ...

  2. mysql一些函数的记录

    今天瞎搞还是弄不出报名程序,偶尔记住了几个mysql函数 mysql_connect()连接数据库 mysql_error()输出文本报错 mysql_select_db()函数设置活动的MySQL  ...

  3. [bzoj1774] [Usaco2009 Dec]Toll 过路费

    Floyd神用法...设dis[i][j]表示i点到j点的最短路(只算边权),map[i][j]表示i到j最小费用 将n个点先按照点权排一下序...这样就可以比较方便的求出路径上最大点权了... 因为 ...

  4. 关于WPF添加右击ContextMeun,以及获取所绑定控件的源

    今天在公司给公司做一个门禁软件,其中有一个添加员工的功能,功能已经做好,但是页面的右边是一个treeView控件,于是我想到再添加员工后,可以在treeview上的部门的TreeViewWithIco ...

  5. 在Linux上如何查看Python3自带的帮助文档?

    俩个步骤: 在Linux终端下输入: ortonwu@ubuntu:~$ pydoc -p 8000 pydoc server ready at http://localhost:8000/ 打开浏览 ...

  6. 访问远程MySQL数据库的方法

    请问各位部署LAMP的时候MySQL是独立出来的服务器,在apache上编译安装php的时候有个--with-mysql后面应该是带mysql路径的,可我应该怎样把这个连接到mysql服务器,因为不是 ...

  7. ios7对于NSString对象进行了的变更

    1.instancetype替代id来做返回值的类型.

  8. eclipse导入项目之后报错

    一.项目本身就有错 二.jdk版本的问题 参考网址:http://jingyan.baidu.com/article/95c9d20da3ec5fec4e756186.html 从别的地方导入一个项目 ...

  9. 5dfda1332b67817b0f2d7839242021ce'Java数据结构和算法

    1.return 一个空的集合,而不是 null 如果一个程序返回一个没有任何值的集合,请确保一个空集合返回,而不是空元素.这样你就不用去写一大堆 "if else" 判断null ...

  10. python_5_模块

    创:5_4_2017 修: 什么是模块? --标准库+第三方库+自定义,为实现某一方面的功能集合(变量,函数,类) 如何安装第三方库? --pip install 第三方库 如何导入和使用模块? -- ...