首页 > 其他分享 >命令模式

命令模式

时间:2022-12-08 16:36:10浏览次数:30  
标签:void 按下 模式 lightRecevier 命令 电灯 按钮 public

智能生活项目需求

命令模式基本介绍

命令模式原理类图

 

 

 

命令模式解决智能生活项目

 

代码实现

 

package com.sky.command;

// 创建命令接口
public interface Command {

    // 执行动作
    void execute();
    // 撤销动作
    void undo();

}

 

package com.sky.command;

// 电灯 接受者
public class LightRecevier {

    public void on(){
        System.out.println("电灯打开了。。。");
    }

    public void off(){
        System.out.println("电灯关闭了。。。");
    }
}
package com.sky.command;

// 电灯 具体命令
public class LightOnCommand implements Command{

      // 聚合LightReceiver
    LightRecevier lightRecevier;

    // 通过构造器 实现注入
    public LightOnCommand(LightRecevier lightRecevier){
        this.lightRecevier = lightRecevier;
    }


    @Override
    public void execute() {
        // 调用接受者的方法 我们可以根据实际情况进行指定,可以是开,也可以是关
        lightRecevier.on();
    }

    @Override
    public void undo() {
        lightRecevier.off();
    }
}
package com.sky.command;

// 电灯 具体命令
public class LightOffCommand implements Command{

    // 聚合LightReceiver
    LightRecevier lightRecevier;

    // 通过构造器 实现注入
    public LightOffCommand(LightRecevier lightRecevier){
        this.lightRecevier = lightRecevier;
    }


    @Override
    public void execute() {
        lightRecevier.off();
    }

    @Override
    public void undo() {
        lightRecevier.on();
    }
}
package com.sky.command;

// 没有任何命令,即空执行:用于初始化每个按钮,当调用空命令的时候,对象什么都不做
// 其实,这也是一种设计模式,可以省掉对空判断
public class NoCommand implements Command {
    @Override
    public void execute() {
        // 空执行
    }

    @Override
    public void undo() {
        // 空执行
    }
}
package com.sky.command;

// 电视机 接受者
public class TVRecevier {

    public void on(){
        System.out.println("电视机打开了。。。");
    }

    public void off(){
        System.out.println("电视机关闭了。。。");
    }
}
package com.sky.command;

// 电视机 具体命令
public class TVOnCommand implements Command{

      // 聚合LightReceiver
    TVRecevier tvRecevier;

    // 通过构造器 实现注入
    public TVOnCommand(TVRecevier tvRecevier){
        this.tvRecevier = tvRecevier;
    }


    @Override
    public void execute() {
        // 调用接受者的方法 我们可以根据实际情况进行指定,可以是开,也可以是关
        tvRecevier.on();
    }

    @Override
    public void undo() {
        tvRecevier.off();
    }
}
package com.sky.command;

// 电视机 具体命令
public class TVOffCommand implements Command{

    // 聚合LightReceiver
    TVRecevier tvRecevier;

    // 通过构造器 实现注入
    public TVOffCommand(TVRecevier tvRecevier){
        this.tvRecevier = tvRecevier;
    }


    @Override
    public void execute() {
        tvRecevier.off();
    }

    @Override
    public void undo() {
        tvRecevier.on();
    }
}
package com.sky.command;

// 调用者
public class RemoteController {

    // 打开按钮命令组
    Command[] onCommands;

    // 关闭按钮命令组
    Command[] offCommands;

    // 撤销按钮命令组
    Command undoCommand;

    // 构造器 初始化为空命令组 new Command[5]; 5表示需要控制的智能家电的数量
    public RemoteController(){
        offCommands = new Command[5];
        onCommands = new Command[5];
        for (int i = 0; i < 5; i++) {
            offCommands[i] = new NoCommand();
            onCommands[i] = new NoCommand();
        }
    }

    // 设置命令
    public void setCommand(int no, Command onCommand, Command offCommand){
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

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

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

    // 撤销命令
    public void undoButtonWasPushed(){
        undoCommand.undo();
    }
}
package com.sky.command;

// 客户端
public class Client {

    public static void main(String[] args) {

        System.out.println("\n\n=============使用遥控器操作电灯==============");
        // 电灯命令接受者
        LightRecevier lightRecevier = new LightRecevier();
        // 电灯打开 关闭命令
        LightOnCommand lightOnCommand = new LightOnCommand(lightRecevier);
        LightOffCommand lightOffCommand = new LightOffCommand(lightRecevier);

        // 遥控器
        RemoteController remoteController = new RemoteController();
        // 设置电灯命令
        remoteController.setCommand(0,lightOnCommand,lightOffCommand);

        // 执行命令 比如:no=0 表示电灯的开和关的操作
        System.out.println("=============按下电灯开的按钮==============");
        remoteController.onButtonWasPushed(0);
        System.out.println("=============按下电灯关的按钮==============");
        remoteController.offButtonWasPushed(0);
        System.out.println("=============按下电灯撤销的按钮==============");
        remoteController.undoButtonWasPushed();
        System.out.println("=============按下电灯关的按钮==============");
        remoteController.offButtonWasPushed(0);


        System.out.println("\n\n=============使用遥控器操作电视机==============");

        // 电灯命令接受者
        TVRecevier tvRecevier = new TVRecevier();
        // 电灯打开 关闭命令
        TVOnCommand tvOnCommand = new TVOnCommand(tvRecevier);
        TVOffCommand tvOffCommand = new TVOffCommand(tvRecevier);

        // 设置电灯命令
        remoteController.setCommand(1,tvOnCommand,tvOffCommand);

        // 执行命令 比如:no=1 表示电视机的开和关的操作
        System.out.println("=============按下电视机开的按钮==============");
        remoteController.onButtonWasPushed(1);
        System.out.println("=============按下电视机关的按钮==============");
        remoteController.offButtonWasPushed(1);
        System.out.println("=============按下电视机撤销的按钮==============");
        remoteController.undoButtonWasPushed();
        System.out.println("=============按下电视机关的按钮==============");
        remoteController.offButtonWasPushed(1);
    }
}

 

=============使用遥控器操作电灯==============
=============按下电灯开的按钮==============
电灯打开了。。。
=============按下电灯关的按钮==============
电灯关闭了。。。
=============按下电灯撤销的按钮==============
电灯打开了。。。
=============按下电灯关的按钮==============
电灯关闭了。。。


=============使用遥控器操作电视机==============
=============按下电视机开的按钮==============
电视机打开了。。。
=============按下电视机关的按钮==============
电视机关闭了。。。
=============按下电视机撤销的按钮==============
电视机打开了。。。
=============按下电视机关的按钮==============
电视机关闭了。。。

命令模式的注意事项和细节

 

标签:void,按下,模式,lightRecevier,命令,电灯,按钮,public
From: https://www.cnblogs.com/dongyaotou/p/16966468.html

相关文章