模式介绍

命令模式(Command Pattern)是一种数据驱动的设计模式。请求以命令的形式包裹在对象中,并传给调用对象,调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。

使用场景

1、认为是命令的地方都可以使用命令模式,比如:GUI 中每一个按钮都是一条命令。
2、系统需要支持命令的执行操作和恢复操作,也可以考虑使用命令模式。

系统实现

/**
 * 电灯家电
 */
public class LightReceiver {
    public void on(){
        System.out.println("电灯打开了!");
    }

    public void off(){
        System.out.println("电灯关闭了!");
    }
}
/**
 * 命令接口
 */
public interface Command {
    public void execute();
    public void undo();
}
/**
 * 空命令对象
 */
public class NoCommand implements Command {
    @Override
    public void execute() {

    }

    @Override
    public void undo() {

    }
}
/**
 * 电灯打开命令
 */
public class LightOnCommand implements Command {
    private LightReceiver lighter;

    public LightOnCommand(LightReceiver lighter){
        this.lighter = lighter;
    }

    @Override
    public void execute() {
        lighter.on();
    }

    @Override
    public void undo() {
        lighter.off();
    }
}
/**
 * 电灯关闭命令
 */
public class LightOffCommand implements Command{
    private LightReceiver lighter;

    public LightOffCommand(LightReceiver lighter){
        this.lighter = lighter;
    }
    @Override
    public void execute() {
        lighter.off();
    }

    @Override
    public void undo() {
        lighter.on();
    }
}
/**
 * 智能遥控器
 */
public class RemoteControllar {
    private Command[] onCommand;
    private Command[] offCommand;
    private Command undoCommand;

    // 遥控器只能操作5个家电
    public RemoteControllar(){
        onCommand = new Command[5];
        offCommand = new Command[5];
        for(int i=0 ; i<5; i++){
            onCommand[i] = new NoCommand();
            offCommand[i] = new NoCommand();
        }
    }

    // 设定命令
    public void setCommand(int NO, Command onCommand, Command offCommand){
        this.onCommand[NO] = onCommand;
        this.offCommand[NO] = offCommand;
    }

    // 打开命令
    public void onButtonWasPushed(int NO){
        onCommand[NO].execute();
        undoCommand = onCommand[NO];
    }

    // 关闭命令
    public void offButtonWasPushed(int NO){
        offCommand[NO].execute();
        undoCommand = offCommand[NO];
    }

    //撤消命令
    public void undoButtonWasPushed(){
        undoCommand.execute();
    }
}
/**
 * 客户端
 */
public class Client {
    public static void main(String args[]){
        LightReceiver ligher = new LightReceiver();
        LightOnCommand lightOnCommand = new LightOnCommand(ligher);
        LightOffCommand lightOffCommand = new LightOffCommand(ligher);
        //初始化远端控制
        RemoteControllar remoteControllar = new RemoteControllar();
        remoteControllar.setCommand(0, lightOnCommand, lightOffCommand);
        //操作命令
        remoteControllar.onButtonWasPushed(0);
        remoteControllar.offButtonWasPushed(0);
        remoteControllar.undoButtonWasPushed();
    }
}
结果:
电灯打开了!
电灯关闭了!
电灯打开了!

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2022-01-05
  • 2021-10-07
  • 2021-11-06
  • 2021-05-15
  • 2021-09-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-12
  • 2022-01-25
  • 2021-07-03
  • 2021-11-22
相关资源
相似解决方案