菜鸟的博客

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

导航

软件设计-Tutorial

类图:

```mermaid
classDiagram
    class User {
        -String name
        -int age
        +User(String, int)
        +String getName()
        +void setName(String)
        +int getAge()
        +void setAge(int)
        +UserMemento saveToMemento()
        +void restoreFromMemento(UserMemento)
    }

    class UserMemento {
        -String name
        -int age
        +UserMemento(String, int)
        +String getName()
        +int getAge()
    }

    class Caretaker {
        -ArrayList~UserMemento~ mementoList
        -int currentIndex
        +void saveMemento(UserMemento)
        +UserMemento undo()
        +UserMemento redo()
    }

    User --> UserMemento : 保存
    User --> Caretaker : 调用
    Caretaker --> UserMemento : 管理

```

源码:

package Tutorial20;

import java.util.ArrayList;

// 用户类
class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // 创建备忘录
    public UserMemento saveToMemento() {
        return new UserMemento(name, age);
    }

    // 从备忘录恢复
    public void restoreFromMemento(UserMemento memento) {
        this.name = memento.getName();
        this.age = memento.getAge();
    }

    @Override
    public String toString() {
        return "User{name='" + name + "', age=" + age + '}';
    }
}

// 备忘录类
class UserMemento {
    private final String name;
    private final int age;

    public UserMemento(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

// 管理类
class Caretaker {
    private final ArrayList<UserMemento> mementoList = new ArrayList<>();
    private int currentIndex = -1;

    // 保存新的备忘录
    public void saveMemento(UserMemento memento) {
        // 移除当前索引后的所有备忘录
        if (currentIndex < mementoList.size() - 1) {
            mementoList.subList(currentIndex + 1, mementoList.size()).clear();
        }
        mementoList.add(memento);
        currentIndex++;
    }

    // 撤销操作
    public UserMemento undo() {
        if (currentIndex <= 0) {
            System.out.println("无法再撤销!");
            return null;
        }
        currentIndex--;
        return mementoList.get(currentIndex);
    }

    // 恢复到下一个状态
    public UserMemento redo() {
        if (currentIndex >= mementoList.size() - 1) {
            System.out.println("无法再恢复!");
            return null;
        }
        currentIndex++;
        return mementoList.get(currentIndex);
    }
}

// 测试类
public class MementoExample {
    public static void main(String[] args) {
        User user = new User("Alice", 20);
        Caretaker caretaker = new Caretaker();

        // 初始状态
        System.out.println("初始状态:" + user);
        caretaker.saveMemento(user.saveToMemento());

        // 修改用户信息并保存状态
        user.setName("Bob");
        user.setAge(25);
        System.out.println("修改后的状态:" + user);
        caretaker.saveMemento(user.saveToMemento());

        // 进一步修改并保存
        user.setName("Charlie");
        user.setAge(30);
        System.out.println("进一步修改后的状态:" + user);
        caretaker.saveMemento(user.saveToMemento());

        // 撤销操作
        user.restoreFromMemento(caretaker.undo());
        System.out.println("撤销一次:" + user);

        user.restoreFromMemento(caretaker.undo());
        System.out.println("再撤销一次:" + user);

        // 恢复操作
        user.restoreFromMemento(caretaker.redo());
        System.out.println("恢复一次:" + user);

        user.restoreFromMemento(caretaker.redo());
        System.out.println("再恢复一次:" + user);
    }
}

 

posted on 2024-11-20 08:35  hhmzd233  阅读(2)  评论(0编辑  收藏  举报