设计模式–02.命令模式

  • 2020 年 2 月 17 日
  • 笔记

命令模式

命令模式是把对象的操作方法分成一个命令,分别去执行。在分布式环境中,熔断和降级组件使用的设计模式就是命令模式。

为了了解什么是设计模式,可以类比下设计一个万能遥控器的设置,遥控器只负责一个方法的调用,真正的方法实现都在对应的电器上面。

使用的时候,只需要对对应的命令和实体进行注册下就可以了。具体的设计类图如下:

具体实现代码分下面几个步骤:

  1. 定义实体方法的约束,也就是当前类实体有哪些方法,比如控制灯和电视,都有开和关的方法
 public interface CommandObj {         void on() ;         void off();   }   
  1. 定义对应的类实体具体的实现方法
 public class Light implements CommandObj {       public void on(){           System.out.println("打开电灯。。。");       }         public void off(){           System.out.println("关闭电灯。。。");       }   }     public class TV implements CommandObj{       public void on(){           System.out.println("打开电视。。。");       }         public void off(){           System.out.println("关闭电视。。。");       }     } 
  1. 定义一个命令执行的约束,来约束所有的Command的执行者需要实现的方法 此处需要注意,在是实现了同一个CommandObj接口的实体,不需要Command的约束,为了Demo的完整性,把Commad接口加上。
 public interface Command {       void execute();   }     public class WrapperOffCommand implements Command {       CommandObj commandObj;         public WrapperOffCommand(CommandObj commandObj){           this.commandObj = commandObj;       }         @Override       public void execute() {           commandObj.off();       }   }     public class WrapperOnCommand implements Command {       CommandObj commandObj;         public WrapperOnCommand(CommandObj commandObj){           this.commandObj = commandObj;       }         @Override       public void execute() {           commandObj.on();       }   }   
  1. 实现Controller的方法,即控制器本身
 public class RemoteController {       ConcurrentHashMap<String,Command> onCommands;       ConcurrentHashMap<String,Command> offCommands;         public RemoteController(){           this.onCommands = new ConcurrentHashMap<>();           this.offCommands = new ConcurrentHashMap<>();       }         public void registerCommand(String key, Command onCommand, Command offCommand){           onCommands.put(key,onCommand);           offCommands.put(key,offCommand);       }         // 按下开按钮       public void onButtonPressed(String key){           onCommands.get(key).execute();       }         // 按下关按钮       public void offButtonPressed(String key){           offCommands.get(key).execute();       }   }   
  1. 控制器的使用
 public static void main(String[] args) {       CommandObj light = new Light();       CommandObj tv = new TV();         Command lightOn = new WrapperOnCommand(light);       Command lightOff = new WrapperOffCommand(light);       Command TVOn = new WrapperOnCommand(tv);       Command TVOff = new WrapperOffCommand(tv);           RemoteController remoteController = new RemoteController();       //注册对应的命令       remoteController.registerCommand(RemoteTypeEnum.Light.toString(), lightOn, lightOff);       //注册对应的命令       remoteController.registerCommand(RemoteTypeEnum.TV.toString(), TVOn, TVOff);           remoteController.onButtonPressed(RemoteTypeEnum.Light.toString());       remoteController.offButtonPressed(RemoteTypeEnum.Light.toString());       remoteController.onButtonPressed(RemoteTypeEnum.TV.toString());       remoteController.offButtonPressed(RemoteTypeEnum.TV.toString());   }

(本文完)

作者:付威 博客地址:http://blog.laofu.online

如有任何知识产权、版权问题或理论错误,还请指正。 本文是付威的网络博客原创,自由转载-非商用-非衍生-保持署名,请遵循:创意共享3.0许可证