`
wangpx
  • 浏览: 202332 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

command patten 读书笔记

阅读更多
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);
 }
}
分享到:
评论

相关推荐

    良葛格DesignPattern学习笔记

    良葛格的《Design Pattern学习笔记》不仅涵盖了经典的GOF设计模式,还额外介绍了几种多线程模式,这使得这份学习笔记成为了一个宝贵的学习资源。下面将对其中的部分设计模式进行详细介绍。 #### 二、GOF设计模式 ...

    java源码解读-DesignPattern:Android源码设计模式解析与实战读书笔记源代码

    Android源码设计模式解析与实战读书笔记源代码 说明: 包名factorypattern.normal表示的是工厂方法模式的普通用法 包名factorypattern.practices表示的是工厂方法模式的常用 包名observerpattern表示的是观察者模式...

    Gof design pattern 中文/英文版+web+学习笔记

    13. **命令模式(Command)**:将请求封装为一个对象,从而使你可以使用不同的请求、队列请求、记录请求日志以及支持可撤销的操作。 14. **解释器模式(Interpreter)**:给定一种语言,定义它的文法表示,并提供一...

    300Java设计模式部分学习笔记

    4. 命令模式(Command Pattern):将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化。 5. 解释器模式(Interpreter Pattern):给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个...

    设计模式整理代码-pattern.zip

    - 命令模式(Command):将请求封装为一个对象,以便使用不同的请求、队列或者日志请求,也可以支持可撤销的操作。 - 解释器模式(Interpreter):给定一种语言,定义它的文法表示,并提供一个解释器来处理该语言...

    java设计模式源码和笔记(第一部分)

    `CommandPattern.java`可能包含命令的创建和执行,笔记会讨论如何实现命令和调用者之间的解耦。 这些设计模式的实现和笔记结合,不仅帮助理解设计模式的基本概念,还提供了实际的编码实践,有助于开发者在Eclipse...

    设计模式笔记总结 含C#示例代码 复习好帮手

    ### 设计模式笔记总结 本篇文章是对一份设计模式学习资料的深入解读,这份资料包含了19种经典的设计模式,并提供了C#示例代码,适用于学习和复习。下面将逐一介绍这些设计模式及其核心概念。 #### 1. 简单工厂模式...

    2009 达内Unix学习笔记

    DOS内部命令 用于退出当前的命令处理器(COMMAND.COM) 恢复前一个命令处理器。 Ctrl+d 跟exit一样效果,表中止本次操作。 logout 当csh时可用来退出,其他shell不可用。 clear 清屏,清除(之前的内容并未删除,只是...

    JAVA经典教材笔记

    - Runtime类的方法:exec(String command)。 - **国际化程序** - 国际化的概念:使程序能够适应不同国家和地区。 - 资源绑定:使用.properties文件。 - **System类** - System类的用途:提供系统级别的操作。 -...

    java设计模式笔记_行为模式借鉴.pdf

    备忘录模式通常与其他设计模式结合使用,如命令模式(Command Pattern)和迭代器模式(Iterator Pattern)。在命令模式中,备忘录可以用来存储命令执行前的状态,以便在需要时撤销操作。在迭代器模式中,备忘录可以...

    linux学习笔记 linux学习笔记

    ### Linux学习笔记知识点详解 #### 一、Ubuntu 8.04 学习入门 **知识点1:系统安装与磁盘格式化** - **安装前准备**:在安装Ubuntu 8.04之前,需要准备好相应的安装介质(如光盘或USB启动盘),并确保计算机硬件...

    精通qtp_自动化测试技术领航第三章学习笔记

    正则表达式类RegExp包含多个属性和方法,如Global属性用于全局匹配,IgnoreCase属性用于忽略大小写,Pattern属性用于定义正则表达式模式。RegExp类还提供了Execute、Replace和Test等方法,分别用于执行匹配搜索、...

    shell学习笔记.docx

    - **管道(pipe)**:`command1 | command2`连接两个命令,`command1`的输出作为`command2`的输入。 3. **重定向操作**: - `1&gt;`重定向标准输出,`2&gt;`重定向标准错误,`0重定向标准输入。 - `/dev/null`常用于...

    Makefile完全笔记

    Makefile完全笔记是关于构建自动化工具Makefile的详尽指南,它帮助开发者高效地管理和编译项目。Makefile定义了一系列的规则,告诉make程序如何构建、编译和链接源代码,形成可执行文件。以下是对Makefile核心概念的...

    尚学堂马士兵linux学习笔记

    - 命令链:`command1 | command2 | command3`,其中命令1的输出作为命令2的输入,依此类推。 17. **服务管理** - 启动FTP服务:`service vsftpd start` - 重启系统:`reboot` - 查看IP地址:`ifconfig` 18. **...

    HttpClient学习笔记参考.pdf

    10. **Command Pattern**:HttpMethods类实现了命令模式,使得并行请求或高效连接复用成为可能。 在使用HttpClient之前,通常需要准备一些基础工作,例如确保Java运行环境(JRE 1.3以上),如果需要HTTPS支持,还...

    linux shell Sed学习笔记

    ### Linux Shell Sed 学习笔记:深入理解与实践 #### Sed 概览 Sed(Stream Editor)是一种功能强大的文本处理工具,适用于Unix/Linux环境下的流编辑操作。它能够读取输入流(如文件或标准输入),进行模式匹配、...

    DesignPattern:设计模式的学习笔记和示例代码

    设计模式是软件工程中的一种最佳实践,它是在特定情境下解决常见问题的经验总结。...在DesignPattern-master这个压缩包中,你可以找到关于这些模式的详细讲解和实例代码,为你的Java开发之旅提供宝贵的参考资料。

    Bash_Shell 小笔记

    **Bash Shell 小笔记** Bash Shell是Unix/Linux操作系统中最常用的一种命令行解释器,全称为Bourne-Again SHell,它是GNU项目的一部分,是Linux系统默认的Shell环境。Bash Shell提供了丰富的命令行交互功能,允许...

    软件体系结构与设计模式课堂笔记.docx

    这篇课堂笔记涵盖了UML、Spring Boot以及设计模式等多个方面的重要概念。 首先,UML(统一建模语言)是软件开发中用于可视化、规格说明、构建和文档化的标准方法。在UML类图中,我们关注的主要关系有依赖、关联、...

Global site tag (gtag.js) - Google Analytics