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. 【JavaScript--String对象】

    JavaScript 字符串(String) 对象 String 对象属性 属性 描述 constructor 对创建该对象的函数的引用 lenght 字符串的长度 prototype 允许您向对象添 ...

  2. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  3. JS 监听浏览器各个标签间的切换

    以前看到过一些网页,在标签切换到其它地址时,网页上的标题上会发生变化,一直不知道这个是怎么做的,最近查了一些资料才发现有一个 visibilitychange 事件就可以搞定,这里将介绍一下页面可见性 ...

  4. RAID 详解

    一.什么是RAID 磁盘阵列全名是『Redundant Arrays of Inexpensive Disks, RAID 』,英翻中的意思是:容错式廉价磁盘阵列. RAID 可以透过一个技术(软件或 ...

  5. happymall 第十一章订单表 数据表设计

    为订单号生成唯一索引,用用户id和订单号生成组合索引提高查询效率.

  6. dig命令

      dig(域信息搜索器)命令是一个用于询问 DNS 域名服务器的灵活的工具.它执行 DNS 搜索,显示从受请求的域名服务器返回的答复.多数 DNS 管理员利用 dig 作为 DNS 问题的故障诊断, ...

  7. 邓_phpcms_二次开发_创建插件

    Phpcms_V9           [test]测试 ================================================================ ====== ...

  8. ProtoBuf 与 gRPC

    用 Protobuf 很久了,但是一直觉得很简单,所以就没有做一个总结,今天想尝试一下 gRPC,顺带就一起总结一下.ProtoBuf 是个老同志了,应该是 2010 的时候发布的,然后被广泛使用,目 ...

  9. JavaScript Date 时间对象方法

    Date(日期/时间对象) Date 操作日期和时间的对象 Date.getDate( ) 返回一个月中的某一天 Date.getDay( ) 返回一周中的某一天 Date.getFullYear( ...

  10. Hyperledger Fabric CouchDB as the State Database

    使用CouchDB作为状态数据库 状态数据库选项 状态数据库包括LevelDB和CouchDB.LevelDB是嵌入在peer进程中的默认键/值状态数据库,CouchDB是一个可选的外部状态数据库.与 ...