前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式--02.命令模式

设计模式--02.命令模式

作者头像
付威
发布2020-02-17 10:30:29
3830
发布2020-02-17 10:30:29
举报

命令模式

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

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

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

IO
IO

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

  1. 定义实体方法的约束,也就是当前类实体有哪些方法,比如控制灯和电视,都有开和关的方法
代码语言:javascript
复制
 public interface CommandObj {
        
     void on() ;
        
     void off();
 }
 
  1. 定义对应的类实体具体的实现方法
代码语言:javascript
复制
 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接口加上。
代码语言:javascript
复制
 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的方法,即控制器本身
代码语言:javascript
复制
 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. 控制器的使用
代码语言:javascript
复制
 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许可证

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 命令模式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档