2023/10/24 每日总结

今天完成了设计模式实验20

实验 20:备忘录模式

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

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

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

 

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

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

源代码

 

复制代码
package org.test20;



import java.util.ArrayList;
import java.util.List;

public class Caretaker {
    private List<Memento> list=new ArrayList<>();
    public Memento getMemento() {
        Memento mm=list.get(list.size()-2);
        list.remove(list.size()-2);
        return mm;
    }
    public void setMemento(Memento memento) {
        list.add(memento);
    }
}



package org.test20;

public class Client {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        UserInfo user=new UserInfo();
        Caretaker c=new Caretaker();
        user.setAccount("zhangsan");
        user.setPassword("123456");
        System.out.println("status1:");
        user.show();
        c.setMemento(user.saveMemento());
        System.out.println("*************");
        user.setPassword("111111");
        System.out.println("status2:");
        user.show();
        c.setMemento(user.saveMemento());
        System.out.println("*************");
        user.setPassword("zyx666");
        System.out.println("status3:");
        user.show();
        c.setMemento(user.saveMemento());
        System.out.println("*************");
        user.setPassword("777777");
        System.out.println("status4:");
        user.show();
        c.setMemento(user.saveMemento());
        System.out.println("*************");
        user.restoreMemento(c.getMemento());
        System.out.println("back to status3:");
        user.show();
        System.out.println("*************");
        user.restoreMemento(c.getMemento());
        System.out.println("back to status2:");
        user.show();
        System.out.println("*************");
        user.restoreMemento(c.getMemento());
        System.out.println("back to status1:");
        user.show();
        System.out.println("*************");

    }
}




package org.test20;

import java.util.ArrayList;
import java.util.List;

public class Memento {
    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(String account, String password, String telNo) {
        this.account = account;
        this.password = password;
        this.telNo = telNo;
    }
}






package org.test20;

public class UserInfo {
    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) {
        this.account=memento.getAccount();
        this.password=memento.getPassword();
        this.telNo=memento.getTelNo();
    }
    public void show() {
        System.out.println("Account:"+this.account);
        System.out.println("Password:"+this.password);
        System.out.println("TelNo:"+this.telNo);
    }
}
复制代码

 

posted @   樱花开到我身边  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示