命令模式-Command Pattern

1. 命令模式

- 将动作的请求者和动作的执行者分开,达到解耦的目的
- 命令的发布者,并不清楚具体事谁来执行
- 具体的请求封装成一个对象, 支持撤销
- 角色:   命令发布者(Invoker)
          命令具体执行者(Reveiver)
          命令(Command) 链接二发布者和执行者
          
- 命令模式的操作相对比较简单,一般只有执行和撤回两个命令
- 空对象模式:当调用空对象的方法的时候,什么也不做,可以省略掉对空判断

在这里插入图片描述

在这里插入图片描述

package com.nike.erick.commond;

import lombok.AllArgsConstructor;

public class Test {
    public static void main(String[] args) {
        RemoteController remoteController = new RemoteController(10);
        remoteController.onButton(0, new LightCommand(new LightReceiver()));
        remoteController.offButton(0, new LightCommand(new LightReceiver()));
    }

}

interface Command {
    /*执行*/
    void execute(String state);

    /*撤回*/
    void rollback(String state);
}

/*具体的命令执行者*/
@AllArgsConstructor
class LightCommand implements Command {

    public Receiver receiver;

    @Override
    public void execute(String state) {
        if (state == "on") {
            receiver.on();
        } else if (state == "off") {
            receiver.off();
        }
    }

    @Override
    public void rollback(String state) {
        if (state == "off") {
            receiver.on();
        } else if (state == "on") {
            receiver.off();
        }
    }
}

/*执行的时候,不会做任何操作: 避免了下面 command的判空处理*/
class BlankCommand implements Command {

    @Override
    public void execute(String state) {
    }

    @Override
    public void rollback(String state) {
    }
}

interface Receiver {
    void on();

    void off();
}

class LightReceiver implements Receiver {
    @Override
    public void on() {
        System.out.println("打开电灯");
    }

    @Override
    public void off() {
        System.out.println("关闭电灯");
    }
}


/*遥控板:用来发送命令的
1. 比如包含了5排开和关的命令,和一个回退的命令*/
class RemoteController {
    /*按照0-5排序*/
    public int commandPairs;
    public Command[] offCommands;
    public Command[] onCommands;
    public Command rollbackCommand; // 撤销的按钮

    /*初始化的时候*/
    public RemoteController(int commandPairs) {
        this.commandPairs = commandPairs;

        if (commandPairs <= 0) {
            throw new IllegalArgumentException("Command Paris Illegal");
        }

        offCommands = new Command[commandPairs];
        onCommands = new Command[commandPairs];

        BlankCommand blankCommand = new BlankCommand();
        rollbackCommand = blankCommand;

        for (int i = 0; i < commandPairs; i++) {
            offCommands[i] = blankCommand;
        }
    }


    /*按下开关的时候,再去初始化对应的Command*/
    public void onButton(int buttonIndex, Command command) {
        onCommands[buttonIndex] = command;
        onCommands[buttonIndex].execute("on");
        /*用于回退的按钮*/
        rollbackCommand = onCommands[buttonIndex];
    }

    /*按下开关的时候,再去初始化对应的Command*/
    public void offButton(int buttonIndex, Command command) {
        offCommands[buttonIndex] = command;
        offCommands[buttonIndex].execute("off");
        /*用于回退的按钮*/
        rollbackCommand = offCommands[buttonIndex];
    }

    public void rollback(String state) {
        rollbackCommand.rollback(state);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值