­

設計模式–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許可證