[img]http://blufiles.storage.live.com/y1pZ1u4rC44uChn_NJcl-uonGNoE99EajJ0tmjJFMjbGqvM1avfL8b2B_rB1XeTq7unlfAHtQQrQ9o[/img]1
。命令模式将发出请求的对象和执行请求的对象解耦。
2。在被解耦的两者之间是通过命令对象进行沟通的。命令对象封装了接受者和一个或者一组动作。
3。调用者通过调用命令对象的execute()发出请求,这会使得接受者的动作被调用。
4 。调用者可以接受命令当作参数,甚至在运行时动态进行。
5。命令可以支持撤销,做法是实现一个undo()方法来回到execute()被执行前的状态。
6。宏命令是命令的一种简单的延伸,允许调用多个命令。宏方法也可以支持撤销。
7。实际操作时,很常见使用“聪明”命令对象,也就是直接实现了请求,而不是将工作委托给接收者。
8。命令也可以用来实现日志和事物系统。

package pattern;public interface Command { public void execute(); public void undo();}class Light{ public void on(){  System.out.println("Light on"); } public void off(){  System.out.println("Light off"); } public  Light(String ms){  System.out.println(ms); } public  Light(){ }}class TV { public void on(){  System.out.println("TV on"); } public void off(){  System.out.println("TV off"); } public  TV(String ms){  System.out.println(ms); } public  TV(){ }}class Hottub{ public void on(){  System.out.println("Hottub on"); } public void off(){  System.out.println("Hottub off"); } public  Hottub(String ms){  System.out.println(ms); } public  Hottub(){ }}class CeilingFan{ public static final int HIGH=3; public static final int  MEDIUM=2; public static final int LOW=1; public static final int OFF=0; String location; int speed;

 public void on(){   } public void off(){     speed=OFF; } public  CeilingFan(String ms){  this.location=location;  speed=OFF;  System.out.println(ms); } public void high(){  speed=HIGH; } public void medium(){  speed=MEDIUM; } public void low(){  speed=LOW; } public int getSpeed(){  return speed; } public  CeilingFan(){

 }}class GarageDoor{ public void up(){  System.out.println("GarageDoor up "); } public void  down(){  System.out.println("GarageDoor down"); }  public void stop(){  System.out.println("GarageDoor stop"); } public void lightOn(){  System.out.println("GarageDoor lightOn"); } public void lightOff(){  System.out.println("GarageDoor LightOff"); } public GarageDoor(){} public GarageDoor(String ms){  System.out.println(ms); }}class Stereo{ public Stereo(){} public Stereo(String ms){  System.out.println(ms); } int volume=0; public void on(){  System.out.println("Stereo on"); } public void off(){  System.out.println("Stereo off"); } public void setCD(){  System.out.println("Stereo CD"); } public void setVolume(int vol){  volume= vol; }}class LightOnCommand implements Command{ Light light;

 public LightOnCommand(Light light){  this.light=light; } public void execute(){  light.on(); } public void undo(){  light.off(); }

}class TVOnCommand implements Command{ TV tv;

 public TVOnCommand(TV tv){  this.tv=tv; } public void execute(){  tv.on(); } public void undo(){  tv.off(); }

}class TVOffCommand implements Command{ TV tv;

 public TVOffCommand(TV tv){  this.tv=tv; } public void execute(){  tv.off(); } public void undo(){  tv.on(); }

}class HottubOnCommand implements Command{ Hottub hottub;

 public HottubOnCommand(Hottub hottub){  this.hottub=hottub; } public void execute(){  hottub.on(); } public void undo(){  hottub.off(); }

}class HottubOffCommand implements Command{ Hottub hottub;

 public HottubOffCommand(Hottub hottub){  this.hottub=hottub; } public void execute(){  hottub.off(); } public void undo(){  hottub.on(); }

}class LightOffCommand implements Command{ Light light; public LightOffCommand(Light light){  this.light=light; } public void execute(){  light.off(); } public void undo(){  light.on(); }}class CeilingFanOnCommand implements Command{ CeilingFan ceilingFan; public CeilingFanOnCommand(CeilingFan ceilingFan){  this.ceilingFan=ceilingFan; } public void execute(){  ceilingFan.on(); } public void undo(){  ceilingFan.off(); }}class CeilingFanHighCommand implements Command{ CeilingFan ceilingFan; int prevSpeed; public CeilingFanHighCommand(CeilingFan ceilingFan){  this.ceilingFan=ceilingFan; } public void execute(){  prevSpeed=ceilingFan.getSpeed();  ceilingFan.high(); } public void undo(){  if(prevSpeed==CeilingFan.HIGH){   ceilingFan.high();  }  if(prevSpeed==CeilingFan.MEDIUM){   ceilingFan.medium();  }  if(prevSpeed==CeilingFan.LOW){   ceilingFan.low();  }  if(prevSpeed==CeilingFan.OFF){   ceilingFan.off();  } }}class CeilingFanMediumCommand implements Command{ CeilingFan ceilingFan; int prevSpeed; public CeilingFanMediumCommand(CeilingFan ceilingFan){  this.ceilingFan=ceilingFan; } public void execute(){  prevSpeed=ceilingFan.getSpeed();  ceilingFan.medium(); } public void undo(){  if(prevSpeed==CeilingFan.HIGH){   ceilingFan.high();  }  if(prevSpeed==CeilingFan.MEDIUM){   ceilingFan.medium();  }  if(prevSpeed==CeilingFan.LOW){   ceilingFan.low();  }  if(prevSpeed==CeilingFan.OFF){   ceilingFan.off();  } }}class CeilingFanLowCommand implements Command{ CeilingFan ceilingFan; int prevSpeed; public CeilingFanLowCommand(CeilingFan ceilingFan){  this.ceilingFan=ceilingFan; } public void execute(){  prevSpeed=ceilingFan.getSpeed();  ceilingFan.low(); } public void undo(){  if(prevSpeed==CeilingFan.HIGH){   ceilingFan.high();  }  if(prevSpeed==CeilingFan.MEDIUM){   ceilingFan.medium();  }  if(prevSpeed==CeilingFan.LOW){   ceilingFan.low();  }  if(prevSpeed==CeilingFan.OFF){   ceilingFan.off();  } }}class CeilingFanOffCommand implements Command{ CeilingFan ceilingFan; int prevSpeed; public CeilingFanOffCommand(CeilingFan ceilingFan){  this.ceilingFan=ceilingFan; } public void execute(){  prevSpeed=ceilingFan.getSpeed();  ceilingFan.off(); } public void undo(){  if(prevSpeed==CeilingFan.HIGH){   ceilingFan.high();  }  if(prevSpeed==CeilingFan.MEDIUM){   ceilingFan.medium();  }  if(prevSpeed==CeilingFan.LOW){   ceilingFan.low();  }  if(prevSpeed==CeilingFan.OFF){   ceilingFan.off();  } }}class GarageDoorUpCommand implements Command{ GarageDoor garageDoor; public GarageDoorUpCommand(GarageDoor garageDoor){  this.garageDoor=garageDoor; } public void execute(){  garageDoor.up(); } public void undo(){  garageDoor.down(); }}class GarageDoorDownCommand implements Command{ GarageDoor garageDoor; public GarageDoorDownCommand(GarageDoor garageDoor){  this.garageDoor=garageDoor; } public void execute(){  garageDoor.down(); } public void undo(){  garageDoor.up(); }}class StereOnCommand implements Command{ Stereo stereo; public StereOnCommand(Stereo stereo){  this.stereo=stereo;   } public void execute(){  stereo.on(); } public void undo(){  stereo.off(); }}class StereOnWithCDCommand implements Command{ Stereo stereo; public StereOnWithCDCommand(Stereo stereo){  this.stereo=stereo;   } public void execute(){  stereo.on();  stereo.setCD();  stereo.setVolume(11); } public void undo(){  stereo.off(); }}class StereOffCommand implements Command{ Stereo stereo; public StereOffCommand(Stereo stereo){  this.stereo=stereo;   } public void execute(){  stereo.off(); } public void undo(){  stereo.on();  stereo.setCD();  stereo.setVolume(11); }}class GarageDoorOpenCommand implements Command{ GarageDoor garageDoor; public GarageDoorOpenCommand(GarageDoor garageDoor){  this.garageDoor=garageDoor; } public void execute(){  this.garageDoor.lightOn(); } public void undo(){  garageDoor.lightOff(); }}class NoCommand implements Command{ public void execute(){} public void undo(){}}class SimpleRemoteControl{ Command slot; public SimpleRemoteControl(){} public void setCommand(Command command){  slot=command; } public void bottomWasPressed(){  slot.execute(); }}class RemoteControl{ Command[] onCommands; Command[] offCommands; 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;  } } public void setCommand(int slot,Command onCommand,Command offCommand){  onCommands[slot]=onCommand;  offCommands[slot]=offCommand; } public void onButtonWasPushed(int slot){  onCommands[slot].execute(); } public void offButtonWasPushed(int slot){  offCommands[slot].execute(); } public String toString(){  StringBuffer stringBuff=new StringBuffer();  stringBuff.append("\n------ Remote Control ------\n");  for(int i=0; i<onCommands.length;i++){   stringBuff.append("[slot "+i+"]"+onCommands[i].getClass().getName()+"  "+offCommands[i].getClass().getName() +"\n");  }  return stringBuff.toString(); }}class RemoteControlWithUndo{ Command[] onCommands; Command[] offCommands; Command undoCommand; public RemoteControlWithUndo(){  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(); } public String toString(){  StringBuffer stringBuff=new StringBuffer();  stringBuff.append("\n------ Remote Control ------\n");  for(int i=0; i<onCommands.length;i++){   stringBuff.append("[slot "+i+"]"+onCommands[i].getClass().getName()+"  "+offCommands[i].getClass().getName() +"\n");  }  return stringBuff.toString(); }}class MacroCommand implements Command{ Command[] commands; public MacroCommand(Command[] commands){  this.commands=commands; } public void execute(){  for (int i=0;i<commands.length;i++){   commands[i].execute();  } } public void undo(){  for (int i=0;i<commands.length;i++){   commands[i].undo();  } }

}

package pattern;public class RemoteLoader { /**  * @param args  */ public static void main(String[] args) {  RemoteControlWithUndo remoteControlundo=new RemoteControlWithUndo();  RemoteControl remoteControl=new RemoteControl();  Light livingRoomLight=new Light("Living Room");        Light kitchenLight=new Light("Kitchen");        CeilingFan ceilingFan=new CeilingFan("Living Room");        GarageDoor garageDoor=new GarageDoor("");        Stereo stereo=new Stereo("Living Room");

        LightOnCommand livingRoomLightOn=new LightOnCommand(livingRoomLight);        LightOffCommand livingRoomLightOff=new LightOffCommand(livingRoomLight);        LightOnCommand kitchenLightOn=new LightOnCommand(kitchenLight);        LightOffCommand kitchenLightOff=new LightOffCommand(kitchenLight);

        CeilingFanOnCommand ceilingFanOn=new CeilingFanOnCommand(ceilingFan);        CeilingFanOffCommand ceilingFanOff=new CeilingFanOffCommand(ceilingFan);        GarageDoorUpCommand garageDoorUp=new GarageDoorUpCommand(garageDoor);        GarageDoorDownCommand garageDoorDown=new GarageDoorDownCommand(garageDoor);

        StereOnWithCDCommand stereoOnWithCD= new StereOnWithCDCommand(stereo);        StereOffCommand stereoOff=new StereOffCommand(stereo);

        remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);        remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);        remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);        remoteControl.setCommand(3, stereoOnWithCD, stereoOff);

        System.out.println(remoteControl);

        remoteControl.onButtonWasPushed(0);        remoteControl.offButtonWasPushed(0);        remoteControl.onButtonWasPushed(1);        remoteControl.offButtonWasPushed(1);        remoteControl.onButtonWasPushed(2);        remoteControl.offButtonWasPushed(2);        remoteControl.onButtonWasPushed(3);        remoteControl.offButtonWasPushed(3);

        remoteControlundo.setCommand(0, livingRoomLightOn, livingRoomLightOff);        remoteControlundo.onButtonWasPushed(0);        remoteControlundo.offButtonWasPushed(0);        System.out.println(remoteControlundo);        remoteControlundo.undoButtonWasPushed();        remoteControlundo.offButtonWasPushed(0);        remoteControlundo.onButtonWasPushed(0);        System.out.println(remoteControlundo);        remoteControlundo.undoButtonWasPushed();

        System.out.println("ceilingFan high beging ......");        CeilingFanMediumCommand ceilingFanMedium=new CeilingFanMediumCommand(ceilingFan);        CeilingFanHighCommand ceilingFanHigh=new CeilingFanHighCommand(ceilingFan);        remoteControlundo.setCommand(0, ceilingFanMedium, ceilingFanOff);        remoteControlundo.setCommand(1, ceilingFanHigh, ceilingFanOff);

        remoteControlundo.onButtonWasPushed(0);        remoteControlundo.offButtonWasPushed(0);        System.out.println(remoteControlundo);        remoteControlundo.undoButtonWasPushed();        remoteControlundo.onButtonWasPushed(1);        System.out.println(remoteControlundo);        remoteControlundo.undoButtonWasPushed();

        System.out.println("hong command begin ......");        Light light =new Light("Living Room");        TV tv=new TV("Living Room");        Hottub hottub=new Hottub();        LightOnCommand lightOn=new LightOnCommand(light);        StereOnCommand stereoOn=new StereOnCommand(stereo);        TVOnCommand tvOn=new TVOnCommand(tv);        HottubOnCommand hottubOn=new HottubOnCommand(hottub);        LightOffCommand lightOff=new LightOffCommand(light);             TVOffCommand tvOff=new TVOffCommand(tv);        HottubOffCommand hottubOff=new HottubOffCommand(hottub);        Command[] partyOn={lightOn,stereoOn,tvOn,hottubOn};        Command[] partyOff={lightOff,stereoOff,tvOff,hottubOff};        MacroCommand partyOnMacro=new MacroCommand(partyOn);        MacroCommand partyOffMacro=new MacroCommand(partyOff);        remoteControlundo.setCommand(0, partyOnMacro, partyOffMacro);        System.out.println(remoteControlundo);        System.out.println("---- Pushing Macro On---");        remoteControlundo.onButtonWasPushed(0);        System.out.println("---- Pushing Macro Off---");        remoteControlundo.offButtonWasPushed(0); }}

command patten 读书笔记相关推荐

  1. The Linux Command Line读书笔记(二)

    第七章: 字符展开: 通过展开,你输入的字符,在 shell 对它起作用之前,会展开成为别的字符. [me@linuxbox ~]$ echo * Desktop Documents ls-outpu ...

  2. Iterator patten 读书笔记

    提供 一种方法 顺序访问 一个聚和 对象中的各个元素,而又不暴露其内部的表示或实现. 迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露其内部的表示.把游走的任务放在迭代器上,而不是聚合上.这样简 ...

  3. Command 模式——读书笔记

    实现类中,存储一个私有的Map对象,该对象中以键值对的方式放置各个命令,当使用时通过传入键参数获取相应的命令,通过调用该命令完成功能实现. 参考:http://caterpillar.onlyfun. ...

  4. 《程序员的呐喊》读书笔记(上)

    <程序员的呐喊>是Google一位老程序员的经验总结,文中展现了他对各大语言如Java.C/C++.Lisp.Python.Ruby.Perl等的极端观点,比如大力吐槽C++,极力推崇C. ...

  5. 《Spring实战》读书笔记--SpringMVC之forward与redirect

    <Spring实战>读书笔记--SpringMVC之forward与redirect 1.forward与redirect介绍 1.1 redirect 重定向,服务器收到请求后发送一个状 ...

  6. 《实现领域驱动设计》读书笔记

    大家好,我是烤鸭:     <实现领域驱动设计>,读书笔记,贴个封面,要不不知道是哪本. 了解概念 刚开始接触DDD,肯定懵逼,很多名词,一点点看下. 领域:带有业务属性的范围,比如搞直播 ...

  7. python权威指南 pdf_Ansible权威指南pdf txt mobi下载及读书笔记

    Ansible权威指南pdf txt mobi下载读书笔记 读书笔记:工作机制:基于openSSH通信,需安装SSH Python,底层基于SSH协议,windows基于PowerShell仅客户侧. ...

  8. The Pragmatic Programmer 读书笔记之中的一个 DRY-Don’t Repeat Youself

     The Pragmatic Programmer读书笔记之中的一个 DRY-Don't Repeat Youself 尽管自己买了非常多软件project方面的书,可是由于时间的问题.一直没有静 ...

  9. 《深入理解Android2》读书笔记(五)

    接上篇<深入理解Android2>读书笔记(四) startActivity Am void run() throws RemoteException {try {printMessage ...

最新文章

  1. 神奇的输入 while(cin....)如何在遇见换行之后进入下一层循环读入
  2. 谷歌新研究:基于数据共享的神经网络快速训练方法
  3. 谷歌魔改Transformer登NeurIPS 2021!一层8个token更好用
  4. 真我新格调 勇敢使梦想×××
  5. python实现简单的api接口-用python写一个restful API
  6. 一款WP小游戏代码分享
  7. 男女之间应该保留多少隐私
  8. 使用AspectJ开发AOP更加便捷,你不知道嘛
  9. linux chrome无法上网,Chromium浏览器(Linux Chrome)无法显示WebGL
  10. 【315天】每日项目总结系列053(2017.12.17)
  11. 跑毒的乌龟-0 : 随机漫步
  12. matlab自动交易系统设计4 随笔
  13. 关于PLC手册中的源型和漏型
  14. 7、什么是三极管(PNP与NPN型)恒流源电路(放电电路与充电电路)
  15. CFLAGS、CXXFLAGS、LDFLAGS与LIBS
  16. 二维数组更改vue,VueX中直接修改数据报错,修改一维数组,二维数组,报错的原因...
  17. 实验室主机Ubuntu远程控制+自动开关机
  18. HTB-Blocky
  19. 洗扑克牌 (乱数排序)
  20. 老鼠出迷宫问题(Java)(递归)

热门文章

  1. 网站风格变黑白的方法,用css或javascript方法将网站改为灰色
  2. [思维模式-9]:《如何系统思考》-5- 认识篇 - 改变开环、组合逻辑的线性思考,实施闭环、时序逻辑的动态思考。
  3. 自动检测文本文件编码是否为GB2312(简体中文),并转换为UTF8编码,附一个GB2312全区对应的utf8编码码表
  4. python爬取喜马拉雅收费_python 爬取喜马拉雅节目生成RSS Feed
  5. CPLD与16C554在航空发动机参数采集器中的应用——转载
  6. PlayStation Classic由开源PCSX模拟器提供支持
  7. 威胁手游安全,这些知识点你要了解看看!
  8. PHP-FFMpeg 安装
  9. linux中.sql.gz文件解压,linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
  10. odoo12 学习: 无效视图 xxxx xxxx xxxx 定义 False