一、

1.因为是操作经常变化,所以封装操作为command对象。You can do that by introducing “command objects” into your design. A command object encapsulates a request to do something (like turn on a light) on a specific object (say, the living room light object). So, if we store a command object for each button, when the button is pressed we ask the command object to do some work. The remote doesn’t have any idea what the work is, it just has a command object that knows how to talk to the right object to get the work done.So, you see, the remote is decoupled from the light object!

2.The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects

with different requests, queue or log requests, and support undoable operations.

3.When you need to decouple an object making requests from the objects that know how to perform the requests, use

the Command Pattern.

3.

4.

5.

6.

7.

8.

二、简单命令模式

1.

package headfirst.designpatterns.command.simpleremote;public interface Command {public void execute();
}

2.

 1 package headfirst.designpatterns.command.simpleremote;
 2
 3 public class LightOnCommand implements Command {
 4     Light light;
 5
 6     public LightOnCommand(Light light) {
 7         this.light = light;
 8     }
 9
10     public void execute() {
11         light.on();
12     }
13 }

3.

 1 package headfirst.designpatterns.command.simpleremote;
 2
 3 public class Light {
 4
 5     public Light() {
 6     }
 7
 8     public void on() {
 9         System.out.println("Light is on");
10     }
11
12     public void off() {
13         System.out.println("Light is off");
14     }
15 }

4.

 1 package headfirst.designpatterns.command.simpleremote;
 2
 3 //
 4 // This is the invoker
 5 //
 6 public class SimpleRemoteControl {
 7     Command slot;
 8
 9     public SimpleRemoteControl() {}
10
11     public void setCommand(Command command) {
12         slot = command;
13     }
14
15     public void buttonWasPressed() {
16         slot.execute();
17     }
18 }

5.

 1 package headfirst.designpatterns.command.simpleremote;
 2
 3 public class RemoteControlTest {
 4     public static void main(String[] args) {
 5         SimpleRemoteControl remote = new SimpleRemoteControl();
 6         Light light = new Light();
 7         GarageDoor garageDoor = new GarageDoor();
 8         LightOnCommand lightOn = new LightOnCommand(light);
 9         GarageDoorOpenCommand garageOpen =
10             new GarageDoorOpenCommand(garageDoor);
11
12         remote.setCommand(lightOn);
13         remote.buttonWasPressed();
14         remote.setCommand(garageOpen);
15         remote.buttonWasPressed();
16     }
17
18 }

三、远程调用命令模式

1.

 1 package headfirst.designpatterns.command.remote;
 2
 3 public class RemoteLoader {
 4
 5     public static void main(String[] args) {
 6         RemoteControl remoteControl = new RemoteControl();
 7
 8         Light livingRoomLight = new Light("Living Room");
 9         Light kitchenLight = new Light("Kitchen");
10         CeilingFan ceilingFan= new CeilingFan("Living Room");
11         GarageDoor garageDoor = new GarageDoor("");
12         Stereo stereo = new Stereo("Living Room");
13
14         LightOnCommand livingRoomLightOn =
15                 new LightOnCommand(livingRoomLight);
16         LightOffCommand livingRoomLightOff =
17                 new LightOffCommand(livingRoomLight);
18         LightOnCommand kitchenLightOn =
19                 new LightOnCommand(kitchenLight);
20         LightOffCommand kitchenLightOff =
21                 new LightOffCommand(kitchenLight);
22
23         CeilingFanOnCommand ceilingFanOn =
24                 new CeilingFanOnCommand(ceilingFan);
25         CeilingFanOffCommand ceilingFanOff =
26                 new CeilingFanOffCommand(ceilingFan);
27
28         GarageDoorUpCommand garageDoorUp =
29                 new GarageDoorUpCommand(garageDoor);
30         GarageDoorDownCommand garageDoorDown =
31                 new GarageDoorDownCommand(garageDoor);
32
33         StereoOnWithCDCommand stereoOnWithCD =
34                 new StereoOnWithCDCommand(stereo);
35         StereoOffCommand  stereoOff =
36                 new StereoOffCommand(stereo);
37
38         remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
39         remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
40         remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);
41         remoteControl.setCommand(3, stereoOnWithCD, stereoOff);
42
43         System.out.println(remoteControl);
44
45         remoteControl.onButtonWasPushed(0);
46         remoteControl.offButtonWasPushed(0);
47         remoteControl.onButtonWasPushed(1);
48         remoteControl.offButtonWasPushed(1);
49         remoteControl.onButtonWasPushed(2);
50         remoteControl.offButtonWasPushed(2);
51         remoteControl.onButtonWasPushed(3);
52         remoteControl.offButtonWasPushed(3);
53     }
54 }

2.

 1 package headfirst.designpatterns.command.remote;
 2
 3 //
 4 // This is the invoker
 5 //
 6 public class RemoteControl {
 7     Command[] onCommands;
 8     Command[] offCommands;
 9
10     public RemoteControl() {
11         onCommands = new Command[7];
12         offCommands = new Command[7];
13
14         Command noCommand = new NoCommand();
15         for (int i = 0; i < 7; i++) {
16             onCommands[i] = noCommand;
17             offCommands[i] = noCommand;
18         }
19     }
20
21     public void setCommand(int slot, Command onCommand, Command offCommand) {
22         onCommands[slot] = onCommand;
23         offCommands[slot] = offCommand;
24     }
25
26     public void onButtonWasPushed(int slot) {
27         onCommands[slot].execute();
28     }
29
30     public void offButtonWasPushed(int slot) {
31         offCommands[slot].execute();
32     }
33
34     public String toString() {
35         StringBuffer stringBuff = new StringBuffer();
36         stringBuff.append("\n------ Remote Control -------\n");
37         for (int i = 0; i < onCommands.length; i++) {
38             stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
39                 + "    " + offCommands[i].getClass().getName() + "\n");
40         }
41         return stringBuff.toString();
42     }
43 }

3.

 1 package headfirst.designpatterns.command.remote;
 2
 3 public class Stereo {
 4     String location;
 5
 6     public Stereo(String location) {
 7         this.location = location;
 8     }
 9
10     public void on() {
11         System.out.println(location + " stereo is on");
12     }
13
14     public void off() {
15         System.out.println(location + " stereo is off");
16     }
17
18     public void setCD() {
19         System.out.println(location + " stereo is set for CD input");
20     }
21
22     public void setDVD() {
23         System.out.println(location + " stereo is set for DVD input");
24     }
25
26     public void setRadio() {
27         System.out.println(location + " stereo is set for Radio");
28     }
29
30     public void setVolume(int volume) {
31         // code to set the volume
32         // valid range: 1-11 (after all 11 is better than 10, right?)
33         System.out.println(location + " Stereo volume set to " + volume);
34     }
35 }

4.

 1 package headfirst.designpatterns.command.remote;
 2
 3 public class StereoOnWithCDCommand implements Command {
 4     Stereo stereo;
 5
 6     public StereoOnWithCDCommand(Stereo stereo) {
 7         this.stereo = stereo;
 8     }
 9
10     public void execute() {
11         stereo.on();
12         stereo.setCD();
13         stereo.setVolume(11);
14     }
15 }

5.

四、有undo功能的命令模式

1.

 1 package headfirst.designpatterns.command.undo;
 2
 3 public class RemoteLoader {
 4
 5     public static void main(String[] args) {
 6         RemoteControlWithUndo remoteControl = new RemoteControlWithUndo();
 7
 8         Light livingRoomLight = new Light("Living Room");
 9
10         LightOnCommand livingRoomLightOn =
11                 new LightOnCommand(livingRoomLight);
12         LightOffCommand livingRoomLightOff =
13                 new LightOffCommand(livingRoomLight);
14
15         remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
16
17         remoteControl.onButtonWasPushed(0);
18         remoteControl.offButtonWasPushed(0);
19         System.out.println(remoteControl);
20         remoteControl.undoButtonWasPushed();
21         remoteControl.offButtonWasPushed(0);
22         remoteControl.onButtonWasPushed(0);
23         System.out.println(remoteControl);
24         remoteControl.undoButtonWasPushed();
25
26         CeilingFan ceilingFan = new CeilingFan("Living Room");
27
28         CeilingFanMediumCommand ceilingFanMedium =
29                 new CeilingFanMediumCommand(ceilingFan);
30         CeilingFanHighCommand ceilingFanHigh =
31                 new CeilingFanHighCommand(ceilingFan);
32         CeilingFanOffCommand ceilingFanOff =
33                 new CeilingFanOffCommand(ceilingFan);
34
35         remoteControl.setCommand(0, ceilingFanMedium, ceilingFanOff);
36         remoteControl.setCommand(1, ceilingFanHigh, ceilingFanOff);
37
38         remoteControl.onButtonWasPushed(0);
39         remoteControl.offButtonWasPushed(0);
40         System.out.println(remoteControl);
41         remoteControl.undoButtonWasPushed();
42
43         remoteControl.onButtonWasPushed(1);
44         System.out.println(remoteControl);
45         remoteControl.undoButtonWasPushed();
46     }
47 }

2.

 1 package headfirst.designpatterns.command.undo;
 2
 3 //
 4 // This is the invoker
 5 //
 6 public class RemoteControlWithUndo {
 7     Command[] onCommands;
 8     Command[] offCommands;
 9     Command undoCommand;
10
11     public RemoteControlWithUndo() {
12         onCommands = new Command[7];
13         offCommands = new Command[7];
14
15         Command noCommand = new NoCommand();
16         for(int i=0;i<7;i++) {
17             onCommands[i] = noCommand;
18             offCommands[i] = noCommand;
19         }
20         undoCommand = noCommand;
21     }
22
23     public void setCommand(int slot, Command onCommand, Command offCommand) {
24         onCommands[slot] = onCommand;
25         offCommands[slot] = offCommand;
26     }
27
28     public void onButtonWasPushed(int slot) {
29         onCommands[slot].execute();
30         undoCommand = onCommands[slot];
31     }
32
33     public void offButtonWasPushed(int slot) {
34         offCommands[slot].execute();
35         undoCommand = offCommands[slot];
36     }
37
38     public void undoButtonWasPushed() {
39         undoCommand.undo();
40     }
41
42     public String toString() {
43         StringBuffer stringBuff = new StringBuffer();
44         stringBuff.append("\n------ Remote Control -------\n");
45         for (int i = 0; i < onCommands.length; i++) {
46             stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
47                 + "    " + offCommands[i].getClass().getName() + "\n");
48         }
49         stringBuff.append("[undo] " + undoCommand.getClass().getName() + "\n");
50         return stringBuff.toString();
51     }
52 }

3.

1 package headfirst.designpatterns.command.undo;
2
3 public class NoCommand implements Command {
4     public void execute() { }
5     public void undo() { }
6 }

4.

 1 package headfirst.designpatterns.command.undo;
 2
 3 public class Light {
 4     String location;
 5     int level;
 6
 7     public Light(String location) {
 8         this.location = location;
 9     }
10
11     public void on() {
12         level = 100;
13         System.out.println("Light is on");
14     }
15
16     public void off() {
17         level = 0;
18         System.out.println("Light is off");
19     }
20
21     public void dim(int level) {
22         this.level = level;
23         if (level == 0) {
24             off();
25         }
26         else {
27             System.out.println("Light is dimmed to " + level + "%");
28         }
29     }
30
31     public int getLevel() {
32         return level;
33     }
34 }

5.

package headfirst.designpatterns.command.undo;public class LightOffCommand implements Command {Light light;int level;public LightOffCommand(Light light) {this.light = light;}public void execute() {level = light.getLevel();light.off();}public void undo() {light.dim(level);}
}

6.

 1 package headfirst.designpatterns.command.undo;
 2
 3 public class LightOnCommand implements Command {
 4     Light light;
 5     int level;
 6     public LightOnCommand(Light light) {
 7         this.light = light;
 8     }
 9
10     public void execute() {
11         level = light.getLevel();
12         light.on();
13     }
14
15     public void undo() {
16         light.dim(level);
17     }
18 }

7.

 1 package headfirst.designpatterns.command.undo;
 2
 3 public class CeilingFan {
 4     public static final int HIGH = 3;
 5     public static final int MEDIUM = 2;
 6     public static final int LOW = 1;
 7     public static final int OFF = 0;
 8     String location;
 9     int speed;
10
11     public CeilingFan(String location) {
12         this.location = location;
13         speed = OFF;
14     }
15
16     public void high() {
17         speed = HIGH;
18         System.out.println(location + " ceiling fan is on high");
19     }
20
21     public void medium() {
22         speed = MEDIUM;
23         System.out.println(location + " ceiling fan is on medium");
24     }
25
26     public void low() {
27         speed = LOW;
28         System.out.println(location + " ceiling fan is on low");
29     }
30
31     public void off() {
32         speed = OFF;
33         System.out.println(location + " ceiling fan is off");
34     }
35
36     public int getSpeed() {
37         return speed;
38     }
39 }

8.

 1 package headfirst.designpatterns.command.undo;
 2
 3 public class CeilingFanHighCommand implements Command {
 4     CeilingFan ceilingFan;
 5     int prevSpeed;
 6
 7     public CeilingFanHighCommand(CeilingFan ceilingFan) {
 8         this.ceilingFan = ceilingFan;
 9     }
10
11     public void execute() {
12         prevSpeed = ceilingFan.getSpeed();
13         ceilingFan.high();
14     }
15
16     public void undo() {
17         if (prevSpeed == CeilingFan.HIGH) {
18             ceilingFan.high();
19         } else if (prevSpeed == CeilingFan.MEDIUM) {
20             ceilingFan.medium();
21         } else if (prevSpeed == CeilingFan.LOW) {
22             ceilingFan.low();
23         } else if (prevSpeed == CeilingFan.OFF) {
24             ceilingFan.off();
25         }
26     }
27 }

9.

 1 package headfirst.designpatterns.command.undo;
 2
 3 public class CeilingFanLowCommand implements Command {
 4     CeilingFan ceilingFan;
 5     int prevSpeed;
 6
 7     public CeilingFanLowCommand(CeilingFan ceilingFan) {
 8         this.ceilingFan = ceilingFan;
 9     }
10
11     public void execute() {
12         prevSpeed = ceilingFan.getSpeed();
13         ceilingFan.low();
14     }
15
16     public void undo() {
17         if (prevSpeed == CeilingFan.HIGH) {
18             ceilingFan.high();
19         } else if (prevSpeed == CeilingFan.MEDIUM) {
20             ceilingFan.medium();
21         } else if (prevSpeed == CeilingFan.LOW) {
22             ceilingFan.low();
23         } else if (prevSpeed == CeilingFan.OFF) {
24             ceilingFan.off();
25         }
26     }
27 }

五、全部关或开

1.

 1 package headfirst.designpatterns.command.party;
 2
 3 public class RemoteLoader {
 4
 5     public static void main(String[] args) {
 6
 7         RemoteControl remoteControl = new RemoteControl();
 8
 9         Light light = new Light("Living Room");
10         TV tv = new TV("Living Room");
11         Stereo stereo = new Stereo("Living Room");
12         Hottub hottub = new Hottub();
13
14         LightOnCommand lightOn = new LightOnCommand(light);
15         StereoOnCommand stereoOn = new StereoOnCommand(stereo);
16         TVOnCommand tvOn = new TVOnCommand(tv);
17         HottubOnCommand hottubOn = new HottubOnCommand(hottub);
18         LightOffCommand lightOff = new LightOffCommand(light);
19         StereoOffCommand stereoOff = new StereoOffCommand(stereo);
20         TVOffCommand tvOff = new TVOffCommand(tv);
21         HottubOffCommand hottubOff = new HottubOffCommand(hottub);
22
23         Command[] partyOn = { lightOn, stereoOn, tvOn, hottubOn};
24         Command[] partyOff = { lightOff, stereoOff, tvOff, hottubOff};
25
26         MacroCommand partyOnMacro = new MacroCommand(partyOn);
27         MacroCommand partyOffMacro = new MacroCommand(partyOff);
28
29         remoteControl.setCommand(0, partyOnMacro, partyOffMacro);
30
31         System.out.println(remoteControl);
32         System.out.println("--- Pushing Macro On---");
33         remoteControl.onButtonWasPushed(0);
34         System.out.println("--- Pushing Macro Off---");
35         remoteControl.offButtonWasPushed(0);
36     }
37 }

2.

 1 package headfirst.designpatterns.command.party;
 2
 3 public class MacroCommand implements Command {
 4     Command[] commands;
 5
 6     public MacroCommand(Command[] commands) {
 7         this.commands = commands;
 8     }
 9
10     public void execute() {
11         for (int i = 0; i < commands.length; i++) {
12             commands[i].execute();
13         }
14     }
15
16     /**
17      * NOTE:  these commands have to be done backwards to ensure
18      * proper undo functionality
19      */
20     public void undo() {
21         for (int i = commands.length -1; i >= 0; i--) {
22             commands[i].undo();
23         }
24     }
25 }

3.

 1 package headfirst.designpatterns.command.party;
 2
 3 //
 4 // This is the invoker
 5 //
 6 public class RemoteControl {
 7     Command[] onCommands;
 8     Command[] offCommands;
 9     Command undoCommand;
10
11     public RemoteControl() {
12         onCommands = new Command[7];
13         offCommands = new Command[7];
14
15         Command noCommand = new NoCommand();
16         for(int i=0;i<7;i++) {
17             onCommands[i] = noCommand;
18             offCommands[i] = noCommand;
19         }
20         undoCommand = noCommand;
21     }
22
23     public void setCommand(int slot, Command onCommand, Command offCommand) {
24         onCommands[slot] = onCommand;
25         offCommands[slot] = offCommand;
26     }
27
28     public void onButtonWasPushed(int slot) {
29         onCommands[slot].execute();
30         undoCommand = onCommands[slot];
31     }
32
33     public void offButtonWasPushed(int slot) {
34         offCommands[slot].execute();
35         undoCommand = offCommands[slot];
36     }
37
38     public void undoButtonWasPushed() {
39         undoCommand.undo();
40     }
41
42     public String toString() {
43         StringBuffer stringBuff = new StringBuffer();
44         stringBuff.append("\n------ Remote Control -------\n");
45         for (int i = 0; i < onCommands.length; i++) {
46             stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
47                 + "    " + offCommands[i].getClass().getName() + "\n");
48         }
49         stringBuff.append("[undo] " + undoCommand.getClass().getName() + "\n");
50         return stringBuff.toString();
51     }
52 }

4.

 1 package headfirst.designpatterns.command.party;
 2
 3 public class CeilingFanHighCommand implements Command {
 4     CeilingFan ceilingFan;
 5     int prevSpeed;
 6
 7     public CeilingFanHighCommand(CeilingFan ceilingFan) {
 8         this.ceilingFan = ceilingFan;
 9     }
10     public void execute() {
11         prevSpeed = ceilingFan.getSpeed();
12         ceilingFan.high();
13     }
14     public void undo() {
15         switch (prevSpeed) {
16             case CeilingFan.HIGH:     ceilingFan.high(); break;
17             case CeilingFan.MEDIUM: ceilingFan.medium(); break;
18             case CeilingFan.LOW:     ceilingFan.low(); break;
19             default:                 ceilingFan.off(); break;
20         }
21     }
22 }

六、命令模式的其他作用

1.queuing requests

2.logging requests

转载于:https://www.cnblogs.com/shamgod/p/5259804.html

HeadFirst设计模式之命令模式相关推荐

  1. 设计模式 之 命令模式

    2019独角兽企业重金招聘Python工程师标准>>> 设计模式 之 命令模式 命令模式比较简单,不过多赘述 为什么需要命令模式 将"行为请求者"与"行 ...

  2. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  3. 23种设计模式之命令模式和策略模式的区别

    文章目录 概述 命令模式 策略模式 相同点 总结 概述 命令模式和策略模式确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的 ...

  4. 设计模式之命令模式(Command)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于如何创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...

  5. plsql执行command命令控制台出现乱码_设计模式系列 — 命令模式

    点赞再看,养成习惯,公众号搜一搜[一角钱技术]关注更多原创技术文章.本文 GitHub org_hejianhui/JavaStudy 已收录,有我的系列文章. 前言 23种设计模式速记 单例(sin ...

  6. 设计模式复习-命令模式

    #pragma once #include "stdafx.h" #include<set> #include<string> #include<io ...

  7. Java 设计模式之命令模式

    一.了解命令模式 1.1 什么是命令模式 命令模式将"请求"封装成对象,以便使用不同的请求.队列或者日志来参数化其他对象.命令模式也支持可撤销的操作.这种说法比较难以理解,换种说法 ...

  8. 设计模式:命令模式(Command)

    欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...

  9. 【设计模式】命令模式

    命令模式:将请求封装在对象中,客户不直接调用某个对象的方法,而是使用命令,将命令传递给拥有方法的对象从而让某一方法被调用.UML图例如以下: 以下是用C++描写的命令模式的一个简单样例: #inclu ...

  10. Android设计模式之——命令模式

    一.介绍 命令模式(Command Pattern),是行为型设计模式之一.命令模式相对于其他的设计模式来说并没有那么多的条条框框,其实它不是一个很"规范"的模式,不过,就是基于这 ...

最新文章

  1. Magento 2中文手册之常见概念解析
  2. Spring Boot + Shiro 集成
  3. 22张令人叹为观止的照片,你所未知的另一面
  4. adf时间作用域_ADF:在任务流终结器中支持bean作用域
  5. xshell 上下左右键乱码和退格键失效
  6. oracle导入dmp清除之前,oracle导入dmp遇到的有关问题
  7. java 拦截html请求参数值_javaweb项目,html文件放在了WebRoot下,如何拦截访问html的请求呀?...
  8. 幻想英雄2-战神再起折扣号新手入门攻略
  9. android中Stub Proxy答疑
  10. mysql数据库无法被其他ip访问的解决方法
  11. VisualBrush
  12. 被3整除判断准则的证明
  13. HDP Hive StorageHandler 下推优化的坑
  14. html5手机app抽奖页面,app H5活动抽奖转盘 前端+后台
  15. 机器学习:kNN算法(一)—— 原理与代码实现(不调用库)
  16. 文笔很差系列4 - Kris Kremo
  17. 295-光纤数据收发 隔离卡 加速计算卡 基于 Kintex-7 XC7K325T的半高PCIe x4双路万兆光纤收发卡
  18. 使用npm ERR network If you are behind a proxy 问题
  19. 怎样迅速搭建运营级直播服务器,用直播源码来完成属于你的专属直播服务
  20. 大数据时代来临,数据应用随处可见

热门文章

  1. Selenium爬虫 -- WebDriver多标签页创建与切换
  2. 前端面试常考的10大排序算法
  3. Docker 入门实践
  4. 01 Django简介
  5. linux常用命令(3)——系统管理1
  6. ajax跨域访问问题
  7. Ctrl + R 后,悲剧咯、、、、
  8. 将现有企业级模板项目从 Visual Studio .NET 2003 迁移到 Visual Studio 2005
  9. Fast DDS Fast DDS主要包括以下内容DDS API、Fast DDS-Gen、RTPS Wire Protocol
  10. 【笔记】关于OpenCV中的去畸变代码