2024.11.28

设计模式实验二十

软件设计                  石家庄铁道大学信息学院

 

实验 20:备忘录模式

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

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

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

 
   

 

 

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

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

实验要求:

1.     画出对应的类图;

 

 

2.     提交源代码;

//Memento.java

public 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.java

import java.util.ArrayList;

 

public class Caretaker {

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

 

    // 保存备忘录

    public void addMemento(Memento memento) {

        mementoList.add(memento);

    }

 

    // 获取并移除最后一个备忘录(多次撤销)

    public Memento getLastMemento() {

        if (!mementoList.isEmpty()) {

            return mementoList.remove(mementoList.size() - 1);

        }

        return null;

    }

}

//UserInfoDTO.java

public class UserInfoDTO {

    private String account;

    private String password;

    private String telNo;

 

    // Getter 和 Setter

    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);

    }

}

//Main.java

public class Main {

    public static void main(String[] args) {

        UserInfoDTO user = new UserInfoDTO();

        Caretaker caretaker = new Caretaker();

 

        // 初始状态

        user.setAccount("User1");

        user.setPassword("pass123");

        user.setTelNo("1234567890");

        caretaker.addMemento(user.saveMemento()); // 保存状态

        System.out.println("Initial State:");

        user.show();

 

        // 修改状态1

        user.setAccount("User2");

        user.setPassword("newpass");

        user.setTelNo("0987654321");

        caretaker.addMemento(user.saveMemento()); // 保存状态

        System.out.println("\nState After Modification:");

        user.show();

 

        // 修改状态2

        user.setAccount("User3");

        user.setPassword("password3");

        user.setTelNo("1122334455");

        caretaker.addMemento(user.saveMemento()); // 保存状态

        System.out.println("\nState After Second Modification:");

        user.show();

 

        // 撤销到上一个状态

        System.out.println("\nUndo to Previous State:");

        user.restoreMemento(caretaker.getLastMemento());

        user.show();

 

        // 再次撤销

        System.out.println("\nUndo to Initial State:");

        user.restoreMemento(caretaker.getLastMemento());

        user.show();

    }

}

3. 注意编程规范。

 

 

posted @ 2024-12-25 10:24  cvjj  阅读(4)  评论(0)    收藏  举报