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

命令模式

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

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

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

IO

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

  1. 定义实体方法的约束,也就是当前类实体有哪些方法,比如控制灯和电视,都有开和关的方法

    1
    2
    3
    4
    5
    6
    7
    public interface CommandObj {

    void on() ;

    void off();
    }

  2. 定义对应的类实体具体的实现方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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("关闭电视。。。");
    }

    }

  3. 定义一个命令执行的约束,来约束所有的Command的执行者需要实现的方法

    此处需要注意,在是实现了同一个CommandObj接口的实体,不需要Command的约束,为了Demo的完整性,把Commad接口加上。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    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();
    }
    }

  4. 实现Controller的方法,即控制器本身

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    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();
    }
    }

  5. 控制器的使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
       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());
    }

作者

付威

发布于

2020-02-04

更新于

2020-09-27

许可协议

评论