经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
命令模式
来源:cnblogs  作者:纳兰小依  时间:2019/10/8 9:28:02  对本文有异议

命令模式关注动作本身,通过将动作封装成对象实现调用者和底层实现相分离。调用者只需要简单的下达命令,然后等待命令完成即可,对底层发生了什么完全不知情。关于命令模式一个很直观的例子就是点餐:当我们点餐时,我们只用关心将选好的菜品下单,然后等待送餐即可,我们不关心饭菜是怎么做的,不关心厨师是男是女。

下面通过一个万能遥控器的例子进一步认识命令模式。

步入物联网时代,很多家电都可以实现远程控制,我们想看电视,听音乐,打扫房间,只需要按一下遥控器上对应的按键,相应的家电就会自动工作。那么这样的一款遥控器要怎样实现呢?现在的场景是,遥控器上的多个按钮对应多个家电,每个家电都有“开”、“关”两个命令。当然,最重要的地方在于,遥控器还必须要能够方便扩展,以后购置新的家电时,只要加一些按钮就可以了。

首先定义命令接口,为简单起见,命令接口里面只有execute方法,因为命令就是要被执行的:

  1. 1 public interface Command {
  2. 2 void execute();
  3. 3 }

遥控器上的每一个按钮都是一个命令,对应不同的电器,所以命令应该有很多种具体类型,假设目前有灯泡、电视、音箱三种家电,电器的定义如下:

  1. 1 // 灯泡
  2. 2 public class Light {
  3. 3 public void on(){
  4. 4 System.out.println("打开电灯。。。");
  5. 5 }
  6. 6
  7. 7 public void off(){
  8. 8 System.out.println("关闭电灯。。。");
  9. 9 }
  10. 10 }
  11. 11
  12. 12 // 电视
  13. 13 public class TV {
  14. 14 public void on(){
  15. 15 System.out.println("打开电视。。。");
  16. 16 }
  17. 17
  18. 18 public void off(){
  19. 19 System.out.println("关闭电视。。。");
  20. 20 }
  21. 21 }
  22. 22
  23. 23 // 音箱
  24. 24 public class LoudspeakerBox {
  25. 25 public void on(){
  26. 26 System.out.println("打开音箱。。。");
  27. 27 }
  28. 28
  29. 29 public void off(){
  30. 30 System.out.println("关闭音箱。。。");
  31. 31 }
  32. 32 }

每种电器都有“开”、“关”两个具体命令,定义如下:

  1. 1 // 开灯命令
  2. 2 public class LightOnCommand implements Command{
  3. 3 Light light;
  4. 4
  5. 5 public LightOnCommand(Light light){
  6. 6 this.light = light;
  7. 7 }
  8. 8
  9. 9 @Override
  10. 10 public void execute() {
  11. 11 light.on();
  12. 12 }
  13. 13 }
  14. 14
  15. 15 // 关灯命令
  16. 16 public class LightOffCommand implements Command{
  17. 17 Light light;
  18. 18
  19. 19 public LightOffCommand(Light light){
  20. 20 this.light = light;
  21. 21 }
  22. 22
  23. 23 @Override
  24. 24 public void execute() {
  25. 25 light.off();
  26. 26 }
  27. 27 }
  28. 28
  29. 29 // 电视和音箱的开关命令与此类型,略去
  30. 30 ...

现在可以看看遥控器的样子了:

  1. 1 // 遥控器
  2. 2 public class RemoteController {
  3. 3 Command[] onCommands;
  4. 4 Command[] offCommands;
  5. 5
  6. 6 public RemoteController(int commandSize){
  7. 7 this.onCommands = new Command[commandSize];
  8. 8 this.offCommands = new Command[commandSize];
  9. 9 }
  10. 10
  11. 11 public void setCommand(int i, Command onCommand, Command offCommand){
  12. 12 onCommands[i] = onCommand;
  13. 13 offCommands[i] = offCommand;
  14. 14 }
  15. 15
  16. 16 // 按下开按钮
  17. 17 public void onButtonPressed(int i){
  18. 18 onCommands[i].execute();
  19. 19 }
  20. 20
  21. 21 // 按下关按钮
  22. 22 public void offButtonPressed(int i){
  23. 23 offCommands[i].execute();
  24. 24 }
  25. 25 }

遥控器针对每一种家电设置了两个开关,按下对应的家电的对应开关,会触发相应的动作。这里只对接了3类家电,实际上完全可以对接任意的家电。现在就需要写个测试类看看遥控器是否正常工作:

  1. 1 public class RemoteControllerTest {
  2. 2 public static void main(String[] args){
  3. 3 Light light = new Light();
  4. 4 TV tv = new TV();
  5. 5 LoudspeakerBox loudspeakerBox = new LoudspeakerBox();
  6. 6 Command lightOn = new LightOnCommand(light);
  7. 7 Command lightOff = new LightOffCommand(light);
  8. 8 Command TVOn = new TVOnCommand(tv);
  9. 9 Command TVOff = new TVOffCommand(tv);
  10. 10 Command LoudspeakerBoxOn = new LoudspeakerBoxOnCommand(loudspeakerBox);
  11. 11 Command LoudspeakerBoxOff = new LoudspeakerBoxOffCommand(loudspeakerBox);
  12. 12
  13. 13 RemoteController remoteController = new RemoteController(3);
  14. 14 remoteController.setCommand(0, lightOn, lightOff);
  15. 15 remoteController.setCommand(1, TVOn, TVOff);
  16. 16 remoteController.setCommand(2, LoudspeakerBoxOn, LoudspeakerBoxOff);
  17. 17
  18. 18 remoteController.onButtonPressed(0);
  19. 19 remoteController.offButtonPressed(0);
  20. 20 remoteController.onButtonPressed(1);
  21. 21 remoteController.offButtonPressed(1);
  22. 22 remoteController.onButtonPressed(2);
  23. 23 remoteController.offButtonPressed(2);
  24. 24 }
  25. 25 }

输出如下:

  1. 打开电灯。。。
  2. 关闭电灯。。。
  3. 打开电视。。。
  4. 关闭电视。。。
  5. 打开音箱。。。
  6. 关闭音箱。。。

遥控器看起来一起顺利。对以上的代码进行整理,可以得出命令模式的类图如下。

 

遥控器对应Client,各种家电的开关命令对应ConcreteCommand,Receiver就是家电。

以上就是命令模式的简单应用,它允许我们将动作封装成命令对象,然后就可以随心所欲地存储、传递和调用它们了。通过命令对象实现调用者和执行者解耦,两者之间通过命令对象间接地进行沟通。

原文链接:http://www.cnblogs.com/NaLanZiYi-LinEr/p/11615257.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号