实验 20:备忘录模式

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

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

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

 

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

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

实验要求:

1. 画出对应的类图;

2. 提交源代码;

3. 注意编程规范。

1、类图

 

1、源代码

package org.example;

import java.util.ArrayList;
import java.util.List;
 
public class Caretaker {
    private List<Memo> list = new ArrayList<>();
 
    public void setMemo(Memo m){
        list.add(m);
    }
 
    public Memo getMemo(){
        Memo m = list.get( list.size() - 2 );
        list.remove( list.size() - 2 );
        return m;
    }
}

package org.example;

public class Client {
    public static void main(String[] args) {
        User user = new User();
        Caretaker caretaker = new Caretaker();
 
        user.setId("20223794");
        user.setPassword("123456");
        caretaker.setMemo(user.saveState());
        System.out.println("状态一");
        user.show();
        System.out.println("************************************************************");
 
        user.setId("20223794");
        user.setPassword("dadvgfb");
        caretaker.setMemo(user.saveState());
        System.out.println("状态二");
        user.show();
        System.out.println("************************************************************");
 
        user.setId("20223794");
        user.setPassword("safdvfv");
        caretaker.setMemo(user.saveState());
        System.out.println("状态三");
        user.show();
        System.out.println("************************************************************");
 
        System.out.println("逐步撤销操作:");
        System.out.println("恢复到状态二");
        user.restoreState(caretaker.getMemo());
        user.show();
        System.out.println("************************************************************");
 
        System.out.println("恢复到状态一");
        user.restoreState(caretaker.getMemo());
        user.show();
    }
}

package org.example;

public class Memo {
    private String id;
    private String password;
 
    public Memo(String id, String password) {
        this.id = id;
        this.password = password;
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
}

package org.example;

public class User {
    private String id;
    private String password;
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public Memo saveState(){
        return new Memo(id,password);
    }
 
    public void restoreState(Memo m){
        this.id = m.getId();
        this.password = m.getPassword();
    }
 
    public void show(){
        System.out.println("id:" + this.id);
        System.out.println("password:" + this.password);
    }
}

 

2、运行截图

 

posted @ 2024-11-20 09:16  不会JAVA的小袁  阅读(16)  评论(0编辑  收藏  举报