资讯专栏INFORMATION COLUMN

命令模式-接收者与执行者解耦和

Sike / 2158人阅读

老板:阿飞,咱们公司又接了个新项目,一个客户,,卧室和客厅很大,电灯电视开关也不好找,所以希望制造一个遥控器来控制一些家具的开启与关闭,目前需要5个按键,卧室的灯,卧室的电视,客厅的灯,客厅的电视,在留一个预备按键。我等会把需求文档给你。
项目组长阿飞:好的,老板
项目组长阿飞:小三,来了个需求,你看下,你先设计一下架构
阿三:好的,飞哥

三天过后:飞哥,好了,你看下

先设计了一个接口,里面包含了,每一个按钮的统一行为

</>复制代码

  1. package com.commandPattern.command;
  2. /**
  3. * @program: testPattern
  4. * @description: 命令接口
  5. * @author: Mr.Yang
  6. * @create: 2018-12-08 13:54
  7. **/
  8. public interface Command {
  9. //执行方法
  10. public void exceute();
  11. }

然后建立了一个对象,代表了空对象,什么操作也不执行

</>复制代码

  1. package com.commandPattern.command.nullCommand;
  2. import com.commandPattern.command.Command;
  3. /**
  4. * @program: testPattern
  5. * @description: 建立一个空对象,在许多设计模式种,都会看到空对象的使用,甚至有些时候,空对象本身也被视为一种设计模式
  6. * @author: Mr.Yang
  7. * @create: 2018-12-08 17:40
  8. **/
  9. public class NullCommand implements Command {
  10. public void exceute() {
  11. System.out.println("什么都不做处理");
  12. }
  13. }

灯的具体类

</>复制代码

  1. package com.commandPattern.entity;
  2. /**
  3. * @program: testPattern
  4. * @description: 灯的具体类
  5. * @author: Mr.Yang
  6. * @create: 2018-12-08 17:31
  7. **/
  8. public class Lamp {
  9. private String name;
  10. /**
  11. * name为灯的具体装饰,即为哪里的灯
  12. * @param name
  13. */
  14. public Lamp(String name){
  15. this.name=name;
  16. }
  17. public void on (){
  18. System.out.println(name+"_灯打开");
  19. }
  20. public void off (){
  21. System.out.println(name+"_灯关闭");
  22. }
  23. }

电视的具体类

</>复制代码

  1. package com.commandPattern.entity;
  2. /**
  3. * @program: testPattern
  4. * @description: 电视的具体类
  5. * @author: Mr.Yang
  6. * @create: 2018-12-08 17:35
  7. **/
  8. public class Tv {
  9. private String name;
  10. public Tv(String name){
  11. this.name=name;
  12. }
  13. public void on (){
  14. System.out.println(name+"_电视打开");
  15. }
  16. public void off(){
  17. System.out.println(name+"_电视关闭");
  18. }
  19. }

关闭灯的具体命令

</>复制代码

  1. package com.commandPattern.command.off;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.entity.Lamp;
  4. /**
  5. * @program: testPattern
  6. * @description: 灯关闭
  7. * @author: Mr.Yang
  8. * @create: 2018-12-08 17:33
  9. **/
  10. public class LampOffCommand implements Command {
  11. Lamp lamp;
  12. public LampOffCommand(Lamp lamp){
  13. this.lamp=lamp;
  14. }
  15. //灯关闭
  16. public void exceute() {
  17. lamp.off();
  18. }
  19. }

打开灯的具体命令

</>复制代码

  1. package com.commandPattern.command.on;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.entity.Lamp;
  4. /**
  5. * @program: testPattern
  6. * @description: 灯打开的命令
  7. * @author: Mr.Yang
  8. * @create: 2018-12-08 17:29
  9. **/
  10. public class LampOnCommand implements Command {
  11. Lamp lamp;
  12. public LampOnCommand(Lamp lamp){
  13. this.lamp=lamp;
  14. }
  15. //灯打开的命令
  16. public void exceute() {
  17. lamp.on();
  18. }
  19. }

电视关闭的具体命令

</>复制代码

  1. package com.commandPattern.command.off;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.entity.Tv;
  4. /**
  5. * @program: testPattern
  6. * @description: 电视关闭
  7. * @author: Mr.Yang
  8. * @create: 2018-12-08 17:36
  9. **/
  10. public class TvOffCommand implements Command {
  11. Tv tv;
  12. public TvOffCommand(Tv tv){
  13. this.tv=tv;
  14. }
  15. public void exceute() {
  16. tv.off();
  17. }
  18. }

电视打开的具体命令

</>复制代码

  1. package com.commandPattern.command.on;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.entity.Tv;
  4. /**
  5. * @program: testPattern
  6. * @description: 电视打开
  7. * @author: Mr.Yang
  8. * @create: 2018-12-08 17:37
  9. **/
  10. public class TvOnCommand implements Command {
  11. Tv tv;
  12. public TvOnCommand(Tv tv){
  13. this.tv=tv;
  14. }
  15. public void exceute() {
  16. tv.on();
  17. }
  18. }

建立一个遥控器

</>复制代码

  1. package com.commandPattern.control;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.command.nullCommand.NullCommand;
  4. import java.util.Arrays;
  5. /**
  6. * @program: testPattern
  7. * @description: 遥控器
  8. * @author: Mr.Yang
  9. * @create: 2018-12-08 17:39
  10. **/
  11. public class RemoteControl {
  12. Command[] onCommand;
  13. Command[] offCommand;
  14. //初始化每个操作为空操作
  15. public RemoteControl(){
  16. onCommand=new Command[5];
  17. offCommand=new Command[5];
  18. NullCommand nullCommand = new NullCommand();
  19. for (int i = 0; i < 5; i++) {
  20. onCommand[i]=nullCommand;
  21. offCommand[i]=nullCommand;
  22. }
  23. }
  24. public void setCommond(int index,Command onCommand,Command offCommand){
  25. this.offCommand[index]=offCommand;
  26. this.onCommand[index]=onCommand;
  27. }
  28. public void clickOn(int index){
  29. onCommand[index].exceute();
  30. }
  31. public void clickOff(int index){
  32. offCommand[index].exceute();
  33. }
  34. /**
  35. * 输出每个按钮的具体代表类
  36. * @return
  37. */
  38. @Override
  39. public String toString() {
  40. StringBuffer sb = new StringBuffer();
  41. for (int i = 0; i < onCommand.length; i++) {
  42. sb.append("[index : "+i+"] ");
  43. sb.append(onCommand[i].getClass().getName());
  44. sb.append(" ");
  45. sb.append(offCommand[i].getClass().getName());
  46. sb.append("
  47. ");
  48. }
  49. return sb.toString();
  50. }
  51. }

测试类

</>复制代码

  1. package com.commandPattern.testPattern;
  2. import com.commandPattern.command.off.LampOffCommand;
  3. import com.commandPattern.command.off.TvOffCommand;
  4. import com.commandPattern.command.on.LampOnCommand;
  5. import com.commandPattern.command.on.TvOnCommand;
  6. import com.commandPattern.control.RemoteControl;
  7. import com.commandPattern.entity.Lamp;
  8. import com.commandPattern.entity.Tv;
  9. /**
  10. * @program: test
  11. * @description:
  12. * @author: Mr.Yang
  13. * @create: 2018-12-08 17:48
  14. **/
  15. public class TestPattern {
  16. public static void main(String[] args) {
  17. RemoteControl remoteControl = new RemoteControl();
  18. /**
  19. * 创建装置到合适位置
  20. */
  21. Tv bedRoomTV = new Tv("卧室");
  22. Tv drawiTV = new Tv("客厅");
  23. Lamp bedRoomLamp = new Lamp("卧室");
  24. Lamp drawiLamp = new Lamp("客厅");
  25. /**
  26. * 创建所有命令操作对象
  27. */
  28. //卧室灯关闭对象
  29. LampOffCommand bedLampOffCommand = new LampOffCommand(bedRoomLamp);
  30. //卧室灯打开对象
  31. LampOnCommand bedLampOnCommand = new LampOnCommand(bedRoomLamp);
  32. //卧室TV关闭对象
  33. TvOffCommand bedTvOffCommand = new TvOffCommand(bedRoomTV);
  34. //卧室TV打开对象
  35. TvOnCommand bedTVcommand = new TvOnCommand(bedRoomTV);
  36. //客厅灯打开对象
  37. LampOnCommand drawLampOnCommand = new LampOnCommand(drawiLamp);
  38. //客厅灯关闭对象
  39. LampOffCommand drawLampOffCommand = new LampOffCommand(drawiLamp);
  40. //客厅TV关闭对象
  41. TvOffCommand drawTVOffCommand = new TvOffCommand(drawiTV);
  42. //客厅TV打开对象
  43. TvOnCommand drawTVOnCommand = new TvOnCommand(drawiTV);
  44. System.out.println("---------------------------------------------未赋值之前------------------------------------------------");
  45. System.out.println(remoteControl);
  46. System.out.println("******************************************************************************************************");
  47. /**
  48. * //将操作对象与卡槽一一对应
  49. */
  50. //赋值卧室灯打开与关闭
  51. remoteControl.setCommond(0,bedLampOnCommand,bedLampOffCommand);
  52. //赋值卧室TV打开与关闭
  53. remoteControl.setCommond(1,bedTVcommand,bedTvOffCommand);
  54. //赋值客厅灯打开与关闭
  55. remoteControl.setCommond(2,drawLampOnCommand,drawLampOffCommand);
  56. //赋值客厅TV打开与关闭
  57. remoteControl.setCommond(3,drawTVOnCommand,drawTVOffCommand);
  58. System.out.println("---------------------------------------------赋值之后------------------------------------------------");
  59. System.out.println(remoteControl);
  60. System.out.println("******************************************************************************************************");
  61. /**
  62. * 测试每一个按钮
  63. */
  64. remoteControl.clickOn(0);
  65. remoteControl.clickOff(0);
  66. remoteControl.clickOn(1);
  67. remoteControl.clickOff(1);
  68. remoteControl.clickOn(2);
  69. remoteControl.clickOff(2);
  70. remoteControl.clickOn(3);
  71. remoteControl.clickOff(3);
  72. }
  73. }

测试结果

</>复制代码

  1. ---------------------------------------------未赋值之前------------------------------------------------
  2. [index : 0] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  3. [index : 1] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  4. [index : 2] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  5. [index : 3] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  6. [index : 4] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  7. ******************************************************************************************************
  8. ---------------------------------------------赋值之后------------------------------------------------
  9. [index : 0] com.commandPattern.command.on.LampOnCommand com.commandPattern.command.off.LampOffCommand
  10. [index : 1] com.commandPattern.command.on.TvOnCommand com.commandPattern.command.off.TvOffCommand
  11. [index : 2] com.commandPattern.command.on.LampOnCommand com.commandPattern.command.off.LampOffCommand
  12. [index : 3] com.commandPattern.command.on.TvOnCommand com.commandPattern.command.off.TvOffCommand
  13. [index : 4] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  14. ******************************************************************************************************
  15. 卧室_灯打开
  16. 卧室_灯关闭
  17. 卧室_电视打开
  18. 卧室_电视关闭
  19. 客厅_灯打开
  20. 客厅_灯关闭
  21. 客厅_电视打开
  22. 客厅_电视关闭

阿三:飞哥,我这里使用的设计模式-命令模式,
将动作执行(LampOnCommand,TvOnCommand……)与接收者(Lamp,Tv)包装到对象里面,对外暴露的只有一个Command接口中的execute方法,其他对象不需要知道那个接收者执行了什么动作,只需要知道调用execute,就能完成一个请求的操作,这个对象,与其他对象没有关联,完全解耦和,如果需要做新增,不需要修改原有代码,拓展接收者类和动作执行类,就能实现功能。

项目组长阿飞:不错,不错,进步很大。
项目组长阿飞:第5个按钮可能需要做一个恢复上一步动作的效果,类似于CTRL+Z这个效果,你再改改把。
阿三:好的。
阿三:飞哥修改好了,你看下

命令接口新增撤销方法

</>复制代码

  1. package com.commandPattern.command;
  2. /**
  3. * @program: testPattern
  4. * @description: 命令接口
  5. * @author: Mr.Yang
  6. * @create: 2018-12-08 13:54
  7. **/
  8. public interface Command {
  9. //执行方法
  10. public void exceute();
  11. //撤销方法
  12. public void revoke();
  13. }

建立一个空对象,实现了撤销方法

</>复制代码

  1. package com.commandPattern.command.nullCommand;
  2. import com.commandPattern.command.Command;
  3. /**
  4. * @program: testPattern
  5. * @description: 建立一个空对象,在许多设计模式种,都会看到空对象的使用,甚至有些时候,空对象本身也被视为一种设计模式
  6. * @author: Mr.Yang
  7. * @create: 2018-12-08 17:40
  8. **/
  9. public class NullCommand implements Command {
  10. public void exceute() {
  11. System.out.println("什么都不做处理");
  12. }
  13. public void revoke() {
  14. System.out.println("什么都不做处理");
  15. }
  16. }

灯关闭实现了撤销方法

</>复制代码

  1. package com.commandPattern.command.off;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.entity.Lamp;
  4. /**
  5. * @program: testPattern
  6. * @description: 灯关闭
  7. * @author: Mr.Yang
  8. * @create: 2018-12-08 17:33
  9. **/
  10. public class LampOffCommand implements Command {
  11. Lamp lamp;
  12. public LampOffCommand(Lamp lamp){
  13. this.lamp=lamp;
  14. }
  15. //灯关闭
  16. public void exceute() {
  17. lamp.off();
  18. }
  19. //执行到这个具体实现类,代表上一步是灯关闭,撤销操作即为灯打开
  20. public void revoke() {
  21. lamp.on();
  22. }
  23. }

灯打开实现了撤销方法

</>复制代码

  1. package com.commandPattern.command.on;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.entity.Lamp;
  4. /**
  5. * @program: testPattern
  6. * @description: 灯打开的命令
  7. * @author: Mr.Yang
  8. * @create: 2018-12-08 17:29
  9. **/
  10. public class LampOnCommand implements Command {
  11. Lamp lamp;
  12. public LampOnCommand(Lamp lamp){
  13. this.lamp=lamp;
  14. }
  15. //灯打开的命令
  16. public void exceute() {
  17. lamp.on();
  18. }
  19. //执行到这个具体实现类,代表上一步是灯打开,撤销操作即为灯关闭
  20. public void revoke() {
  21. lamp.off();
  22. }
  23. }

电视关闭实现了撤销方法

</>复制代码

  1. package com.commandPattern.command.off;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.entity.Tv;
  4. /**
  5. * @program: testPattern
  6. * @description: 电视关闭
  7. * @author: Mr.Yang
  8. * @create: 2018-12-08 17:36
  9. **/
  10. public class TvOffCommand implements Command {
  11. Tv tv;
  12. public TvOffCommand(Tv tv){
  13. this.tv=tv;
  14. }
  15. public void exceute() {
  16. tv.off();
  17. }
  18. //执行到这个具体实现类,代表上一步是电视关闭,撤销操作即为电视打开
  19. public void revoke() {
  20. tv.on();
  21. }
  22. }

电视打开实现了撤销方法

</>复制代码

  1. package com.commandPattern.command.on;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.entity.Tv;
  4. /**
  5. * @program: testPattern
  6. * @description: 电视打开
  7. * @author: Mr.Yang
  8. * @create: 2018-12-08 17:37
  9. **/
  10. public class TvOnCommand implements Command {
  11. Tv tv;
  12. public TvOnCommand(Tv tv){
  13. this.tv=tv;
  14. }
  15. public void exceute() {
  16. tv.on();
  17. }
  18. //执行到这个具体实现类,代表上一步是电视打开,撤销操作即为电视关闭
  19. public void revoke() {
  20. tv.off();
  21. }
  22. }

遥控器类,新增变量记录上一步操作

</>复制代码

  1. package com.commandPattern.control;
  2. import com.commandPattern.command.Command;
  3. import com.commandPattern.command.nullCommand.NullCommand;
  4. import java.util.Arrays;
  5. /**
  6. * @program: testPattern
  7. * @description: 遥控器
  8. * @author: Mr.Yang
  9. * @create: 2018-12-08 17:39
  10. **/
  11. public class RemoteControl {
  12. Command[] onCommand;
  13. Command[] offCommand;
  14. //这个变量来记录上一个命令
  15. Command upStepCommand;
  16. //初始化每个操作为空操作
  17. public RemoteControl(){
  18. onCommand=new Command[5];
  19. offCommand=new Command[5];
  20. NullCommand nullCommand = new NullCommand();
  21. for (int i = 0; i < 5; i++) {
  22. onCommand[i]=nullCommand;
  23. offCommand[i]=nullCommand;
  24. }
  25. upStepCommand=nullCommand;
  26. }
  27. public void setCommond(int index,Command onCommand,Command offCommand){
  28. this.offCommand[index]=offCommand;
  29. this.onCommand[index]=onCommand;
  30. }
  31. //新增upStepCommand记录上一步命令
  32. public void clickOn(int index){
  33. onCommand[index].exceute();
  34. upStepCommand=onCommand[index];
  35. }
  36. //新增upStepCommand记录上一步命令
  37. public void clickOff(int index){
  38. offCommand[index].exceute();
  39. upStepCommand=offCommand[index];
  40. }
  41. public void toUpStepClick(){
  42. System.out.println("---撤销---");
  43. upStepCommand.revoke();
  44. }
  45. /**
  46. * 输出每个按钮的具体代表类
  47. * @return
  48. */
  49. @Override
  50. public String toString() {
  51. StringBuffer sb = new StringBuffer();
  52. for (int i = 0; i < onCommand.length; i++) {
  53. sb.append("[index : "+i+"] ");
  54. sb.append(onCommand[i].getClass().getName());
  55. sb.append(" ");
  56. sb.append(offCommand[i].getClass().getName());
  57. sb.append("
  58. ");
  59. }
  60. return sb.toString();
  61. }
  62. }

测试类新增撤销测试

</>复制代码

  1. package com.commandPattern.testPattern;
  2. import com.commandPattern.command.off.LampOffCommand;
  3. import com.commandPattern.command.off.TvOffCommand;
  4. import com.commandPattern.command.on.LampOnCommand;
  5. import com.commandPattern.command.on.TvOnCommand;
  6. import com.commandPattern.control.RemoteControl;
  7. import com.commandPattern.entity.Lamp;
  8. import com.commandPattern.entity.Tv;
  9. /**
  10. * @program: test
  11. * @description:
  12. * @author: Mr.Yang
  13. * @create: 2018-12-08 17:48
  14. **/
  15. public class TestPattern {
  16. public static void main(String[] args) {
  17. RemoteControl remoteControl = new RemoteControl();
  18. /**
  19. * 创建装置到合适位置
  20. */
  21. Tv bedRoomTV = new Tv("卧室");
  22. Tv drawiTV = new Tv("客厅");
  23. Lamp bedRoomLamp = new Lamp("卧室");
  24. Lamp drawiLamp = new Lamp("客厅");
  25. /**
  26. * 创建所有命令操作对象
  27. */
  28. //卧室灯关闭对象
  29. LampOffCommand bedLampOffCommand = new LampOffCommand(bedRoomLamp);
  30. //卧室灯打开对象
  31. LampOnCommand bedLampOnCommand = new LampOnCommand(bedRoomLamp);
  32. //卧室TV关闭对象
  33. TvOffCommand bedTvOffCommand = new TvOffCommand(bedRoomTV);
  34. //卧室TV打开对象
  35. TvOnCommand bedTVcommand = new TvOnCommand(bedRoomTV);
  36. //客厅灯打开对象
  37. LampOnCommand drawLampOnCommand = new LampOnCommand(drawiLamp);
  38. //客厅灯关闭对象
  39. LampOffCommand drawLampOffCommand = new LampOffCommand(drawiLamp);
  40. //客厅TV关闭对象
  41. TvOffCommand drawTVOffCommand = new TvOffCommand(drawiTV);
  42. //客厅TV打开对象
  43. TvOnCommand drawTVOnCommand = new TvOnCommand(drawiTV);
  44. System.out.println("---------------------------------------------未赋值之前------------------------------------------------");
  45. System.out.println(remoteControl);
  46. System.out.println("******************************************************************************************************");
  47. /**
  48. * //将操作对象与卡槽一一对应
  49. */
  50. //赋值卧室灯打开与关闭
  51. remoteControl.setCommond(0,bedLampOnCommand,bedLampOffCommand);
  52. //赋值卧室TV打开与关闭
  53. remoteControl.setCommond(1,bedTVcommand,bedTvOffCommand);
  54. //赋值客厅灯打开与关闭
  55. remoteControl.setCommond(2,drawLampOnCommand,drawLampOffCommand);
  56. //赋值客厅TV打开与关闭
  57. remoteControl.setCommond(3,drawTVOnCommand,drawTVOffCommand);
  58. System.out.println("---------------------------------------------赋值之后------------------------------------------------");
  59. System.out.println(remoteControl);
  60. System.out.println("******************************************************************************************************");
  61. /**
  62. * 测试每一个按钮
  63. */
  64. remoteControl.clickOn(0);
  65. remoteControl.clickOff(0);
  66. //撤销一次
  67. remoteControl.toUpStepClick();
  68. System.out.println("
  69. ");
  70. //撤销一次
  71. remoteControl.toUpStepClick();
  72. System.out.println("
  73. ");
  74. remoteControl.clickOn(1);
  75. remoteControl.clickOff(1);
  76. //撤销一次
  77. remoteControl.toUpStepClick();
  78. System.out.println("
  79. ");
  80. remoteControl.clickOn(2);
  81. remoteControl.clickOff(2);
  82. //撤销一次
  83. remoteControl.toUpStepClick();
  84. System.out.println("
  85. ");
  86. remoteControl.clickOn(3);
  87. remoteControl.clickOff(3);
  88. //撤销一次
  89. remoteControl.toUpStepClick();
  90. System.out.println("
  91. ");
  92. }
  93. }

修改之后的测试结果

</>复制代码

  1. ---------------------------------------------未赋值之前------------------------------------------------
  2. [index : 0] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  3. [index : 1] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  4. [index : 2] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  5. [index : 3] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  6. [index : 4] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  7. ******************************************************************************************************
  8. ---------------------------------------------赋值之后------------------------------------------------
  9. [index : 0] com.commandPattern.command.on.LampOnCommand com.commandPattern.command.off.LampOffCommand
  10. [index : 1] com.commandPattern.command.on.TvOnCommand com.commandPattern.command.off.TvOffCommand
  11. [index : 2] com.commandPattern.command.on.LampOnCommand com.commandPattern.command.off.LampOffCommand
  12. [index : 3] com.commandPattern.command.on.TvOnCommand com.commandPattern.command.off.TvOffCommand
  13. [index : 4] com.commandPattern.command.nullCommand.NullCommand com.commandPattern.command.nullCommand.NullCommand
  14. ******************************************************************************************************
  15. 卧室_灯打开
  16. 卧室_灯关闭
  17. ---撤销---
  18. 卧室_灯打开
  19. ---撤销---
  20. 卧室_灯打开
  21. 卧室_电视打开
  22. 卧室_电视关闭
  23. ---撤销---
  24. 卧室_电视打开
  25. 客厅_灯打开
  26. 客厅_灯关闭
  27. ---撤销---
  28. 客厅_灯打开
  29. 客厅_电视打开
  30. 客厅_电视关闭
  31. ---撤销---
  32. 客厅_电视打开

项目组长阿飞:不错,不错,以后给你涨工资。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72613.html

相关文章

  • js设计模式 --- 命令设计模式

    摘要:命令设计模式涉及三类对象抽象命令类抽象命令类一般是一个抽象类或接口,在其中声明了用于执行请求的等方法,通过这些方法可以调用请求接收者的相关操作。 命令模式 命令模式定义为:Encapsulate a request as an object,there by letting you parameterize clients with different requests,queue o...

    iflove 评论0 收藏0
  • JavaScript设计模式(七):命令模式

    摘要:支持撤销,队列,宏命令等功能。宏命令宏命令一组命令集合命令模式与组合模式的产物发布者发布一个请求,命令对象会遍历命令集合下的一系列子命令并执行,完成多任务。 showImg(https://segmentfault.com/img/bVbu3CN?w=800&h=600); 命令模式:请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相...

    lixiang 评论0 收藏0
  • python 设计模式-命令模式

    摘要:命令模式先来看下命令模式的定义命令模式将请求封装成对象,以便使用不同的请求队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。通过新增两个方法,命令模式能够支持这一点。 命令模式 题目: 现在要做一个智能家居控制遥控器,功能如下图所示。 showImg(https://segmentfault.com/img/remote/1460000012774337?w=1730&h=1...

    tomlingtm 评论0 收藏0
  • python设计模式

    摘要:在本节实验中,我们学习了四种设计模式策略模式,观察者模式,命令模式以及模板方法模式。这四种设计模式都是行为型模式。这就是适配器模式。下面让我们看看适配器模式在实验楼中使用吧。准确来说,装饰者模式能动态的给对象添加行为。 1、策略模式 策略模式将各种操作(算法)进行封装,并使它们之间可以互换。互换的意思是说可以动态改变对象的操作方式(算法)。 -- coding: utf-8 -- im...

    array_huang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<