11.21

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

 

实验 20:备忘录模式

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

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

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

 

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

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

实验要求:

1. 类图;

 

 

2. 源代码;

①、Memento.java

package org.example.shiyanershi;

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 getPassword() {
        return password;
    }


    public String getTelNo() {
        return telNo;
    }

    public String getAccount() {
        return account;
    }
}

 

②、Caretaker.java

package org.example.shiyanershi;

public class Caretaker {
    private Memento[] array = null;
    private int index;
    private Memento memento;


    public Caretaker() {
        array = new Memento[10];
        index = 0;
    }

    public void addMemento(Memento memento) {
        array[index++] = memento;
    }

    public Memento preMemento() {
        Memento pop = null;
        try {
            pop = array[--index];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pop;
    }
}

 

③、Originator.java

package org.example.shiyanershi;

public class Originator  {
    private String account;
    private String password;
    private String telNo;
    public Originator() {
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setTelNo(String telNo) {
        this.telNo = telNo;
    }
    public Memento saveMemento() {
        return new Memento(account, password, telNo);
    }
    public void restoreMemento(Memento memento) {
        this.account = memento.getAccount();
        this.password = memento.getPassword();
        this.telNo = memento.getTelNo();
    }
    public void show() {
        System.out.println("账号:" + this.account + " 密码:" + this.password + " 手机号:" + this.telNo);
    }
}

 

④、Main.java

package org.example.shiyanershi;

public class Main {
    public static void main(String[] args) {
        Originator user = new Originator();
        Caretaker c = new Caretaker();
        user.setAccount("zhangsan");
        user.setPassword("123456");
        user.setTelNo("130000");
        System.out.println("状态一:");
        user.show();
        c.addMemento(user.saveMemento());
        System.out.println("-----------------");

        user.setPassword("111111");
        user.setTelNo("130111");
        System.out.println("状态二:");
        user.show();
        c.addMemento(user.saveMemento());
        System.out.println("-----------------");

        user.setPassword("222222");
        user.setTelNo("130222");
        System.out.println("状态三:");
        user.show();
        System.out.println("-----------------");

        user.restoreMemento(c.preMemento());
        System.out.println("回到状态二:");
        user.show();
        System.out.println("-----------------");

        user.restoreMemento(c.preMemento());
        System.out.println("回到状态一:");
        user.show();
        System.out.println("-----------------");
    }
}

 

3. 运行结果

 

 

 

posted @ 2024-12-18 17:49  七安。  阅读(5)  评论(0编辑  收藏  举报