菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 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 31 1 2 3 4 5

统计

软件设计-Tutorial16

复制代码
```mermaid
classDiagram
    Command <|-- AddCommand
    CommandManager o-- Command
    CommandManager --> Calculator

    class Command {
        <<interface>>
        +execute() void
        +undo() void
    }

    class AddCommand {
        -int value
        -int previousValue
        -Calculator calculator
        +execute() void
        +undo() void
    }

    class Calculator {
        -int result
        +add(int value) void
        +getResult() int
        +setResult(int result) void
    }

    class CommandManager {
        -Stack~Command~ executedCommands
        -Stack~Command~ undoneCommands
        +executeCommand(Command command) void
        +undo() void
        +redo() void
    }

```
复制代码
复制代码
package Tutorial16;

import java.util.Stack;

// 1. Command接口,定义执行和撤销方法
interface Command {
    void execute();
    void undo();
}

// 2. AddCommand具体命令类,模拟加法操作
class AddCommand implements Command {
    private int value;
    private int previousValue;
    private Calculator calculator;

    public AddCommand(Calculator calculator, int value) {
        this.calculator = calculator;
        this.value = value;
    }

    @Override
    public void execute() {
        previousValue = calculator.getResult();
        calculator.add(value);
    }

    @Override
    public void undo() {
        calculator.setResult(previousValue);
    }
}

// 3. Calculator类,存储结果
class Calculator {
    private int result = 0;

    public void add(int value) {
        result += value;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }
}

// 4. CommandManager类,管理命令的执行和撤销
class CommandManager {
    private Stack<Command> executedCommands = new Stack<>();
    private Stack<Command> undoneCommands = new Stack<>();

    public void executeCommand(Command command) {
        command.execute();
        executedCommands.push(command);
        undoneCommands.clear(); // 每次执行新命令时清空撤销栈
    }

    public void undo() {
        if (!executedCommands.isEmpty()) {
            Command command = executedCommands.pop();
            command.undo();
            undoneCommands.push(command);
        }
    }

    public void redo() {
        if (!undoneCommands.isEmpty()) {
            Command command = undoneCommands.pop();
            command.execute();
            executedCommands.push(command);
        }
    }
}

// 5. 测试类
public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        CommandManager manager = new CommandManager();

        // 执行几个加法命令
        manager.executeCommand(new AddCommand(calculator, 10));
        manager.executeCommand(new AddCommand(calculator, 20));

        System.out.println("当前结果: " + calculator.getResult()); // 30

        // 撤销最后一个命令
        manager.undo();
        System.out.println("撤销后结果: " + calculator.getResult()); // 10

        // 重做最后一个撤销的命令
        manager.redo();
        System.out.println("重做后结果: " + calculator.getResult()); // 30
    }
}
复制代码

 

posted on   hhmzd233  阅读(4)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2023-11-13 读后感:《程序员修炼之道》第五部分 - 短期规划
点击右上角即可分享
微信分享提示