11月8日

[实验任务一]:多次撤销和重复的命令模式

某系统需要提供一个命令集合(注:可以使用链表,栈等集合对象实现),用于存储一系列命令对象,并通过该命令集合实现多次undo()和redo()操作,可以使用加法运算来模拟实现。

实验要求:

1. 提交类图;

2. 提交源代码;

3. 注意编程规范。

实验内容:

  1. 类图:

 

 

  1. 源代码:

package org.example;

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

// 接收者类
class Receiver {
    public void action() {
        System.out.println("接收方操作。");
    }
}

// 具体命令类
abstract class ConcreteCommand implements Command {
    protected Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }
}

class ConcreteCommand1 extends ConcreteCommand {
    private int state;

    public ConcreteCommand1(Receiver receiver) {
        super(receiver);
        this.state = 0;
    }

    @Override
    public void execute() {
        state = 1;
        receiver.action();
        System.out.println("执行 ConcreteCommand1");
    }

    @Override
    public void undo() {
        if (state == 1) {
            System.out.println("ConcreteCommand1 已撤消");
            state = 0;
        }
    }

    @Override
    public void redo() {
        if (state == 0) {
            receiver.action();
            System.out.println("ConcreteCommand1 重做");
            state = 1;
        }
    }
}

class ConcreteCommand2 extends ConcreteCommand {
    private int state;

    public ConcreteCommand2(Receiver receiver) {
        super(receiver);
        this.state = 0;
    }

    @Override
    public void execute() {
        state = 1;
        receiver.action();
        System.out.println("执行 ConcreteCommand2");
    }

    @Override
    public void undo() {
        if (state == 1) {
            System.out.println("ConcreteCommand2 已撤消");
            state = 0;
        }
    }

    @Override
    public void redo() {
        if (state == 0) {
            receiver.action();
            System.out.println("重做 ConcreteCommand2");
            state = 1;
        }
    }
}

// 调用者类
class Invoker {
    private Command onStack;
    private Command offStack;

    public void storeAndExecute(Command command) {
        command.execute();
        onStack = command;
        offStack = null;
    }

    public void undo() {
        if (onStack != null) {
            onStack.undo();
            offStack = onStack;
            onStack = null;
        }
    }

    public void redo() {
        if (offStack != null) {
            offStack.redo();
            onStack = offStack;
            offStack = null;
        }
    }
}
// 客户端测试代码
public class CommandPatternDemo {
    public static void main(String[] args) {
        Receiver receiver = new Receiver();
        Invoker invoker = new Invoker();

        Command command1 = new ConcreteCommand1(receiver);
        Command command2 = new ConcreteCommand2(receiver);

        invoker.storeAndExecute(command1); // 1
        invoker.storeAndExecute(command2); // 2

        invoker.undo(); // back to 1
        invoker.undo(); // back to initial state

        invoker.redo(); // move to 1
        invoker.redo(); // move to 2

        invoker.undo(); // back to 1
        invoker.storeAndExecute(command1); // 1
    }
}

 

运行截图:

posted @ 2024-11-08 20:17  序章0  阅读(8)  评论(0编辑  收藏  举报