命令模式(行为型)
来源:互联网 发布:淘宝乒乓球店 编辑:程序博客网 时间:2024/06/01 10:10
命令模式
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。
适用性
1.抽象出待执行的动作以参数化某个对象。
2.在不同的时刻指定、排列和执行请求。
3.支持取消操作。
4.支持修改日志,这样当系统崩溃时,修改可以被重做一遍。
5.用构建在原语操作上的高层操作构造一个系统。
结构类图
headfirst结构类图
-----------------------------------------------------------------
系统结构图
----------------------------------------------------------------------------------------------------------------------
抽象命令
---------------------------------------------------------------------------
public interface Command {public void execute();}
---------------------------------------------------------------------------
具体命令
---------------------------------------------------------------------------
public class DoorOffCommand implements Command {private Door door;public DoorOffCommand(Door door) {this.door = door;}@Overridepublic void execute() {// TODO Auto-generated method stubdoor.doorOff();}}public class DoorOnCommand implements Command{private Door door;public DoorOnCommand(Door door) {this.door = door;}@Overridepublic void execute() {// TODO Auto-generated method stubdoor.doorOn();}}public class LightOffCommand implements Command {private Light light;public LightOffCommand(Light light) {this.light = light;}@Overridepublic void execute() {// TODO Auto-generated method stublight.lightOff();}}public class LightOnCommand implements Command{private Light light;public LightOnCommand(Light light) {this.light = light;}@Overridepublic void execute() {// TODO Auto-generated method stublight.lightOn();}}
---------------------------------------------------------------------------
命令控制器
---------------------------------------------------------------------------------------
public class SimpleRemoteControl {private Command[] onCommand;private Command[] offCommand;private Command undocommand;public SimpleRemoteControl(){onCommand=new Command[2];offCommand=new Command[2];}public void setCommand(int slot,Command onCommand,Command offCommand){this.onCommand[slot]=onCommand;this.offCommand[slot]=offCommand;}public void buttonDoPress(int slot){onCommand[slot].execute();undocommand=onCommand[slot];}public void buttonOffDoPress(int slot){offCommand[slot].execute();undocommand=onCommand[slot];}public void unDoCommand(){undocommand.execute();}}
---------------------------------------------------------------------------------------
命令执行者
---------------------------------------------------------------------------
public class Door {public Door(){}public void doorOff(){System.out.println("Door off");}public void doorOn(){System.out.println("Door on");}}public class Light {public Light(){}public void lightOn(){System.out.println("light on");}public void lightOff(){System.out.println("light off");}}
---------------------------------------------------------------------------
客户
---------------------------------------------------------------------------
public class TestClient {public static void main(String[] args) {Door door=new Door();Light light=new Light();Command command=new DoorOffCommand(door);Command command2=new DoorOnCommand(door);Command command3=new LightOffCommand(light);Command command4=new LightOnCommand(light);SimpleRemoteControl simpleRemoteControl =new SimpleRemoteControl();simpleRemoteControl.setCommand(0, command, command2);simpleRemoteControl.setCommand(1, command3, command4);System.out.println("--------------");simpleRemoteControl.buttonDoPress(0);simpleRemoteControl.buttonOffDoPress(0);simpleRemoteControl.unDoCommand();System.out.println("--------------");simpleRemoteControl.buttonDoPress(1);simpleRemoteControl.buttonOffDoPress(1);simpleRemoteControl.unDoCommand();System.out.println("--------------");}}
---------------------------------------------------------------------------
执行结果
---------------------------------------------------------------------------------------
--------------Door offDoor onDoor off--------------light offlight onlight off--------------
0 0
- 行为型模式-命令
- 行为型--命令模式
- 命令模式(行为型)
- 行为型模式----命令模式
- 行为型模式-命令模式
- 行为型模式--命令模式
- 行为型之命令模式
- Command命令(行为型模式)
- 行为型模式-命令command
- 命令模式(行为型)
- 行为型模式-命令(command)
- 设计模式 - 行为型模式 - 命令模式
- 设计模式->行为型模式->命令模式
- 设计模式--行为型--命令模式
- 行为型模式-----命令模式(Command)
- [设计模式-行为型]命令模式(Command)
- (行为型模式五)命令模式
- 行为型模式-Command ( 命令模式 )
- 迭代的快速排序(Iterative Quick Sort)
- js截取字符串的学习,我的第一个博客文章
- RNN以及LSTM的介绍和公式梳理
- iOS小demo之验证码计时器
- 稀疏编码
- 命令模式(行为型)
- 动态规划-基本思想
- iOS自定义滚动条
- Log4j2学习成果
- SQL Server 存储过程
- iOS开发Debug之单个页面强制横屏
- 【NOIP2010】关押罪犯
- 全排列
- weka 3.6.13-SNAPSHOT 过滤器StringToWordVector参数含义解释