设计模式-命令模式

本文介绍了设计模式中的命令模式,用于封装请求为对象,便于管理和撤销操作。通过Light和Temperature对象,展示了如何创建及执行命令,如LightOnCommand和TemperatureLOWCommand,并通过Control对象管理这些命令,实现灵活的控制和历史记录功能。
摘要由CSDN通过智能技术生成

设计模式-命令模式

1.概述

命令模式将“请求”或者“命令”封装成对象,以便使用不同的请求,队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。

2.类图

在这里插入图片描述

3.角色

Command接口:所有命令或请求都得实现该接口。

Control对象:管理一组命令对象,通过队列或者集合管理命令。

Temperature对象: 具体的组件对象。

TempreatureLOWCommand对象:将Temperature对象温度调低的命令对象,内部组和了一个要操作的Temperature对象。

4.测试

1.接口Command
public interface Command {
    void execute();
    void undo();
}
2.Light
public class Light {

    boolean isON = false;

    public void on(){
        isON = true;
        System.out.println("打开电灯");
    }

    public void off(){
        isON = false;
        System.out.println("关闭电灯");
    }

}
3.Temperature
public class Temperature {
    public static int HIGH = 3;
    public static int MEDIUM = 2;
    public static int LOW = 1;
    public static int OFF = 0;
    int temperature;

    public void high(){
        temperature = HIGH;
        System.out.println("temperature is HIGH");
    }
    public void medium(){
        temperature = MEDIUM;
        System.out.println("temperature is MEDIUM");
    }
    public void low(){
        temperature = LOW;
        System.out.println("temperature is LOW");
    }
    public void off(){
        temperature = OFF;
        System.out.println("temperature is OFF");
    }

    public int getTemperature() {
        return temperature;
    }
}
4.对Light的操作命令对象
public class LightOnComand implements Command{
    Light light;

    public LightOnComand(Light light) {
        this.light = light;
    }

    public void execute() {
        light.on();
    }

    public void undo(){
        light.off();
    }

}
public class LightOffCommand implements Command{

    Light light;


    public LightOffCommand(Light light) {
        this.light = light;
    }

    public void execute() {
        light.off();
    }

    public void undo() {
        light.on();
    }
}
5.对Temperature的操作命令对象
public class TemperatureHIGHCommand implements Command{
    Temperature temperature;
    int preTemperature; //记录执行之前的temperature

    public TemperatureHIGHCommand(Temperature temperature) {
        this.temperature = temperature;
    }

    public void execute() {
        preTemperature = temperature.getTemperature();
        temperature.high();
    }

    public void undo() {
        if(preTemperature==Temperature.MEDIUM){
            temperature.medium();
        }else if(preTemperature==Temperature.LOW){
            temperature.low();
        }else if(preTemperature==Temperature.OFF){
            temperature.off();
        }else{
            // 什么也不做,因为本来也是HIGH
        }
    }
}
public class TemperatureLOWCommand implements Command{
    Temperature temperature;
    int preTemperature;
    public TemperatureLOWCommand(Temperature temperature) {
        this.temperature = temperature;
    }

    public void execute() {
        preTemperature = temperature.getTemperature();
        temperature.low();
    }

    public void undo() {
        if(preTemperature==Temperature.MEDIUM){
            temperature.medium();
        }else if(preTemperature==Temperature.HIGH){
            temperature.high();
        }else if(preTemperature==Temperature.OFF){
            temperature.off();
        }else{
            // 什么也不做,因为本来也是LOW
        }
    }
}
public class TemperaTureMEDIUMCommand implements Command{
    Temperature temperature;
    int preTemperature; //记录执行之前的temperature

    public TemperaTureMEDIUMCommand(Temperature temperature) {
        this.temperature = temperature;
    }

    public void execute() {
        preTemperature = temperature.getTemperature();
        temperature.medium();
    }

    public void undo() {
        if(preTemperature==Temperature.HIGH){
            temperature.high();
        }else if(preTemperature==Temperature.LOW){
            temperature.low();
        }else if(preTemperature==Temperature.OFF){
            temperature.off();
        }else{
            // 什么也不做,因为本来也是MEDIUM
        }
    }
}
public class TemperatureOFFCommand implements Command{
    Temperature temperature;
    int preTemperature; //记录执行之前的temperature

    public TemperatureOFFCommand(Temperature temperature) {
        this.temperature = temperature;
    }

    public void execute() {
        preTemperature = temperature.getTemperature();
        temperature.off();
    }

    public void undo() {
        if(preTemperature==Temperature.MEDIUM){
            temperature.medium();
        }else if(preTemperature==Temperature.LOW){
            temperature.low();
        }else if(preTemperature==Temperature.HIGH){
            temperature.high();
        }else{
            // 什么也不做,因为本来也是OFF
        }
    }
}
6.noCommand表示无操作对象
public class noCommand implements Command{

    public void execute() {

    }

    public void undo() {

    }
}
7.Control管理Command
public class Control {
    Command [] onCommands;   //保存所有的打开命令集合
    Command [] offCommands;  //保存所有的关闭命令集合
    Command undoCommand;    //记录undo日志

    public Control() {
        this.onCommands = new Command[5];
        this.offCommands = new Command[5];
        this.undoCommand = new noCommand();
    }

    public void setCommand(int slot,Command onCommand,Command offCommand) {
        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
    }

    public void onCommandWasExecuted(int slot){
        onCommands[slot].execute();
        undoCommand = onCommands[slot];
    }
    public void offCommandWasExecuted(int slot){
        offCommands[slot].execute();
        undoCommand = offCommands[slot];
    }

    public void undoCommandWasExecuted(){
        undoCommand.undo();
        undoCommand = new noCommand();
    }
}
8.测试main方法
public class Main {
    public static void main(String[] args) {
        Light light  = new Light();
        Temperature temperature = new Temperature();

        LightOnComand lightOnComand = new LightOnComand(light);
        LightOffCommand lightOffCommand = new LightOffCommand(light);

        TemperatureHIGHCommand temperatureHIGHCommand = new TemperatureHIGHCommand(temperature);
        TemperaTureMEDIUMCommand temperaTureMEDIUMCommand = new TemperaTureMEDIUMCommand(temperature);
        TemperatureLOWCommand temperatureLOWCommand = new TemperatureLOWCommand(temperature);
        TemperatureOFFCommand temperatureOFFCommand = new TemperatureOFFCommand(temperature);

        Command noCommand = new noCommand();

        Control myHomeControl = new Control();
        myHomeControl.setCommand(0,lightOnComand,lightOffCommand);
        myHomeControl.setCommand(1,temperatureHIGHCommand,temperatureOFFCommand);
        myHomeControl.setCommand(2,temperaTureMEDIUMCommand,temperatureOFFCommand);
        myHomeControl.setCommand(3,temperatureLOWCommand,temperatureOFFCommand);

        myHomeControl.onCommandWasExecuted(0);
        myHomeControl.onCommandWasExecuted(1);
        myHomeControl.onCommandWasExecuted(3);

        myHomeControl.undoCommandWasExecuted();
    }
}

5.结果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值