JAVA设计模式---命令模式
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设计模式---命令模式的更多相关文章
- 【设计模式】Java设计模式 - 命令模式
Java设计模式 - 命令模式 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 目录 Ja ...
- JAVA 设计模式 命令模式
用途 命令模式 (Command) 将一个请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化:对请求排队或请求日志,以及支持可撤销的操作. 命令模式是一种行为型模式. 结构
- Java设计模式-命令模式(Command)
命令模式很好理解,举个例子,司令员下令让士兵去干件事情,从整个事情的角度来考虑,司令员的作用是,发出口令,口令经过传递,传到了士兵耳朵里,士兵去执行.这个过程好在,三者相互解耦,任何一方都不用去依赖其 ...
- Java设计模式—命令模式
命令模式是一个高内聚的模式. 定义如下:将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能. 通用类图如下: 角色说明: ● Re ...
- Java设计模式の命令模式
意图: 将一个请求封装为一个对象,从而可用不同的请求对客户进行参数化:对请求排队或记录日志,以及支持可撤销的操作 动机: 将”发出请求的对象”和”接收与执行这些请求的对象”分隔开来. 效果: 1).c ...
- linkin大话设计模式--命令模式
linkin大话设计模式--命令模式 首先考虑一种应用情况,某个方法需要完成某一个功能,这个功能的大部分功能已经确定了,但是有可能少量的步骤没法确定,必须等到执行这个方法才可以确定. 也就是说,我们写 ...
- Java设计模式——组合模式
JAVA 设计模式 组合模式 用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模 ...
- java设计模式--单列模式
java设计模式--单列模式 单列模式定义:确保一个类只有一个实例,并提供一个全局访问点. 下面是几种实现单列模式的Demo,每个Demo都有自己的优缺点: Demo1: /** * 单列模式需要满足 ...
- 3.java设计模式-建造者模式
Java设计模式-建造者模式 在<JAVA与模式>一书中开头是这样描述建造(Builder)模式的: 建造模式是对象的创建模式.建造模式可以将一个产品的内部表象(internal repr ...
随机推荐
- (转)Docker之Compose服务编排
转自:https://www.cnblogs.com/52fhy/p/5991344.html Compose是Docker的服务编排工具,主要用来构建基于Docker的复杂应用,Compose 通过 ...
- HDU 2502 月之数(二进制,规律)
月之数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- Codeforces Round #449 (Div. 2)-897A.Scarborough Fair(字符替换水题) 897B.Chtholly's request(处理前一半) 897C.Nephren gives a riddle(递归)
A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- android小说阅读源码、bilibili源码、MVP新闻源码等
Android精选源码 一款基于 MVP+RxJava2+Retrofit2 的应用--熊猫眼 android 五子棋源码分享 android实现全国地图点击效果 android实现立体图案绘制的代码 ...
- 深入设计电子计算器(一)——CPU框架及指令集设计
版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/8278418.html 作者:窗户 Q ...
- RPM包效验
- ABB安全区域(全局区域)的指令解析
VAR wztemporary wzone;//VAR:变量 //wztemporary:全局区域数据类型(wztemporary临时全局区域数据.wzstationary固定式全局区域)wzone: ...
- Spark算子--foreach和foreachPartition
转载请标明出处http://www.cnblogs.com/haozhengfei/p/6776fe93f754daf60d00d2cb509422a1.html foreach和foreachPar ...
- https和http有什么区别
在URL前加https://前缀表明是用SSL加密的. 你的电脑与服务器之间收发的信息传输将更加安全. Web服务器启用SSL需要获得一个服务器证书并将该证书与要使用SSL的服务器绑定. http和h ...
- [SinGuLaRiTy] 复习模板-高精度模板
[SinGuLaRiTy-1042] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 结构体封装 //高精度运算 注意%I64d与%lld # ...