24.12.04

实验 20:备忘录模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解备忘录模式的动机,掌握该模式的结构;

2、能够利用备忘录模式解决实际问题。

[实验任务一]:多次撤销

改进课堂上的“用户信息操作撤销”实例,使得系统可以实现多次撤销(可以使用HashMap、ArrayList等集合数据结构实现)。

实验要求:

1.      画出对应的类图;

2.      提交源代码;

3. 注意编程规范。

 

  1. 类图

 

 

 

 

  1. 源代码:

import java.util.ArrayList;

import java.util.List;

 

// Memento类

class Memento {

    private String account;

    private String password;

    private String telNo;

 

    public Memento(String account, String password, String telNo) {

        this.account = account;

        this.password = password;

        this.telNo = telNo;

    }

 

    public String getAccount() {

        return account;

    }

 

    public String getPassword() {

        return password;

    }

 

    public String getTelNo() {

        return telNo;

    }

}

 

// Caretaker类

class Caretaker {

    private List<Memento> mementoList = new ArrayList<>();

    private int currentIndex = -1;  // 当前状态索引

 

    public void addMemento(Memento memento) {

        // 如果当前状态不是最新状态,则移除当前索引后的历史记录

        if (currentIndex != mementoList.size() - 1) {

            mementoList = mementoList.subList(0, currentIndex + 1);

        }

        mementoList.add(memento);

        currentIndex++;

    }

 

    public Memento undo() {

        if (currentIndex <= 0) {

            System.out.println("无法撤销,已是初始状态。");

            return null;

        }

        currentIndex--;

        return mementoList.get(currentIndex);

    }

 

    public Memento redo() {

        if (currentIndex >= mementoList.size() - 1) {

            System.out.println("无法恢复,已是最新状态。");

            return null;

        }

        currentIndex++;

        return mementoList.get(currentIndex);

    }

 

    public Memento getCurrentMemento() {

        if (currentIndex >= 0 && currentIndex < mementoList.size()) {

            return mementoList.get(currentIndex);

        }

        return null;

    }

}

 

// UserInfoDTO类

public class UserInfoDTO {

    private String account;

    private String password;

    private String telNo;

 

    public String getAccount() {

        return account;

    }

 

    public void setAccount(String account) {

        this.account = account;

    }

 

    public String getPassword() {

        return password;

    }

 

    public void setPassword(String password) {

        this.password = password;

    }

 

    public String getTelNo() {

        return telNo;

    }

 

    public void setTelNo(String telNo) {

        this.telNo = telNo;

    }

 

    public Memento saveMemento() {

        return new Memento(account, password, telNo);

    }

 

    public void restoreMemento(Memento memento) {

        if (memento != null) {

            this.account = memento.getAccount();

            this.password = memento.getPassword();

            this.telNo = memento.getTelNo();

        }

    }

 

    public void show() {

        System.out.println("Account: " + account);

        System.out.println("Password: " + password);

        System.out.println("TelNo: " + telNo);

    }

}

 

// Client类

public class Client {

    public static void main(String[] args) {

        UserInfoDTO user = new UserInfoDTO();

        Caretaker caretaker = new Caretaker();

 

        // 初始状态

        user.setAccount("zhangsan");

        user.setPassword("123456");

        user.setTelNo("13000000000");

        caretaker.addMemento(user.saveMemento());

 

        System.out.println("状态一:");

        user.show();

        System.out.println("---------------------------");

 

        // 修改状态

        user.setPassword("111111");

        user.setTelNo("13100001111");

        caretaker.addMemento(user.saveMemento());

 

        System.out.println("状态二:");

        user.show();

        System.out.println("---------------------------");

 

        // 再次修改状态

        user.setPassword("222222");

        user.setTelNo("13200002222");

        caretaker.addMemento(user.saveMemento());

 

        System.out.println("状态三:");

        user.show();

        System.out.println("---------------------------");

 

        // 多次撤销

        user.restoreMemento(caretaker.undo());

        System.out.println("撤销一次:");

        user.show();

        System.out.println("---------------------------");

 

        user.restoreMemento(caretaker.undo());

        System.out.println("撤销两次:");

        user.show();

        System.out.println("---------------------------");

 

        // 恢复状态

        user.restoreMemento(caretaker.redo());

        System.out.println("恢复一次:");

        user.show();

        System.out.println("---------------------------");

 

        user.restoreMemento(caretaker.redo());

        System.out.println("恢复到最新状态:");

        user.show();

        System.out.println("---------------------------");

    }

}

posted on 2024-12-04 21:36  Daniel350  阅读(6)  评论(0)    收藏  举报