11.23

实验16优化
1.
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ Client │ │ Invoker │ │ Receiver │
│ │ │ │ │ │
│ │ │ -commands: │ │ -value: int │
│ │ │ List │ │
│ │ │ -history: │ │ +add(int) │
│ │ │ Stack │ +getValue() │
│ │ │ -redoStack: │ │ │
│ │ │ Stack │ │
│ │ │ │ │ │
│ +main() │ │ +executeCmd()│ │ │
│ │ │ +undo() │ │ │
│ │ │ +redo() │ │ │
└─────────────┘ └──────────────┘ └─────────────┘
│ │
│ │
▼ │
┌──────────────────┐ │
│ Command │ │
│ │ │
│ +execute() │ │
│ +undo() │──────────────┘
│ +redo() │
└──────────────────┘


┌──────────────────┼──────────────────┐
│ │
┌─────────────┐ ┌─────────────┐
│ AddCommand │ │ Composite │
│ │ │ Command │
│ -receiver │ │ │
│ -operand │ │ -commands: │
│ │ │ List
│ +execute() │ │ │
│ +undo() │ │ +execute() │
│ +redo() │ │ +undo() │
└─────────────┘ │ +redo() │
└─────────────┘
2.
import java.util.*;

// 命令接口
interface Command {
void execute();
void undo();
void redo();
}

// 接收者 - 计算器
class Calculator {
private int value = 0;

public void add(int operand) {
    this.value += operand;
    System.out.println("执行加法: " + operand + ", 当前值: " + value);
}

public void subtract(int operand) {
    this.value -= operand;
    System.out.println("执行减法: " + operand + ", 当前值: " + value);
}

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

}

// 具体命令 - 加法命令
class AddCommand implements Command {
private Calculator calculator;
private int operand;

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

@Override
public void execute() {
    calculator.add(operand);
}

@Override
public void undo() {
    calculator.subtract(operand);
    System.out.println("撤销加法: " + operand + ", 当前值: " + calculator.getValue());
}

@Override
public void redo() {
    calculator.add(operand);
    System.out.println("重做加法: " + operand + ", 当前值: " + calculator.getValue());
}

@Override
public String toString() {
    return "AddCommand{operand=" + operand + "}";
}

}

// 复合命令 - 支持批量操作
class CompositeCommand implements Command {
private List commands = new ArrayList<>();

public void addCommand(Command command) {
    commands.add(command);
}

@Override
public void execute() {
    for (Command command : commands) {
        command.execute();
    }
}

@Override
public void undo() {
    // 逆序撤销
    for (int i = commands.size() - 1; i >= 0; i--) {
        commands.get(i).undo();
    }
}

@Override
public void redo() {
    for (Command command : commands) {
        command.redo();
    }
}

}

// 调用者 - 命令管理器
class CommandManager {
private List commands = new ArrayList<>();
private Stack history = new Stack<>();
private Stack redoStack = new Stack<>();
private Calculator calculator;

public CommandManager(Calculator calculator) {
    this.calculator = calculator;
}

// 执行命令
public void executeCommand(Command command) {
    command.execute();
    commands.add(command);
    history.push(command);
    redoStack.clear(); // 执行新命令时清空重做栈
    System.out.println("命令已执行");
}

// 撤销
public void undo() {
    if (!history.isEmpty()) {
        Command command = history.pop();
        command.undo();
        redoStack.push(command);
        System.out.println("撤销完成");
    } else {
        System.out.println("无法撤销,历史记录为空");
    }
}

// 重做
public void redo() {
    if (!redoStack.isEmpty()) {
        Command command = redoStack.pop();
        command.redo();
        history.push(command);
        System.out.println("重做完成");
    } else {
        System.out.println("无法重做,重做记录为空");
    }
}

// 显示状态
public void displayStatus() {
    System.out.println("\n=== 当前状态 ===");
    System.out.println("计算器值: " + calculator.getValue());
    System.out.println("历史记录: " + history);
    System.out.println("重做栈: " + redoStack);
    System.out.println("================\n");
}

}

// 客户端
public class CommandPatternDemo {
public static void main(String[] args) {
// 创建接收者
Calculator calculator = new Calculator();

    // 创建调用者
    CommandManager manager = new CommandManager(calculator);
    
    // 显示初始状态
    manager.displayStatus();
    
    // 执行一系列加法命令
    System.out.println("=== 执行命令序列 ===");
    manager.executeCommand(new AddCommand(calculator, 10));
    manager.executeCommand(new AddCommand(calculator, 5));
    manager.executeCommand(new AddCommand(calculator, 3));
    
    manager.displayStatus();
    
    // 测试撤销功能
    System.out.println("=== 测试撤销功能 ===");
    manager.undo(); // 撤销 +3
    manager.undo(); // 撤销 +5
    
    manager.displayStatus();
    
    // 测试重做功能
    System.out.println("=== 测试重做功能 ===");
    manager.redo(); // 重做 +5
    manager.redo(); // 重做 +3
    
    manager.displayStatus();
    
    // 测试复合命令
    System.out.println("=== 测试复合命令 ===");
    CompositeCommand composite = new CompositeCommand();
    composite.addCommand(new AddCommand(calculator, 2));
    composite.addCommand(new AddCommand(calculator, 4));
    composite.addCommand(new AddCommand(calculator, 6));
    
    manager.executeCommand(composite);
    manager.displayStatus();
    
    // 撤销复合命令
    System.out.println("=== 撤销复合命令 ===");
    manager.undo();
    manager.displayStatus();
    
    // 最终状态
    System.out.println("=== 最终状态 ===");
    System.out.println("计算器最终值: " + calculator.getValue());
}
posted @ 2025-11-23 23:47  山蚯  阅读(1)  评论(0)    收藏  举报