大三打卡(10.18)

实验 20:备忘录模式

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

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

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

 
   

 


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

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

实验要求:

1.     画出对应的类图;

 

2.     提交源代码;

package RJSJ.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 RJSJ.test20;

 

public class Client {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        UserInfoDTO user=new UserInfoDTO();

        Caretaker c=new Caretaker();

 

        user.setAccount("zhangsan");

        user.setPassword("123456");

        user.setTelNo("1310000000");

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

        user.show();

        c.setMemento(user.saveMemento());

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

 

        user.setPassword("111111");

        user.setTelNo("1310001111");

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

        user.show();

        c.setMemento(user.saveMemento());

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

 

        user.setPassword("zyx666");

        user.setTelNo("15733333333");

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

        user.show();

        c.setMemento(user.saveMemento());

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

 

        user.setPassword("777777");

        user.setTelNo("15511111111");

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

        user.show();

        c.setMemento(user.saveMemento());

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

 

        user.setPassword("666666");

        user.setTelNo("17455555555");

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

        user.show();

        c.setMemento(user.saveMemento());

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

 

 

        user.restoreMemento(c.getMemento());

        System.out.println("回到状态四:");

        user.show();

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

 

        user.restoreMemento(c.getMemento());

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

        user.show();

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

 

        user.restoreMemento(c.getMemento());

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

        user.show();

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

 

        user.restoreMemento(c.getMemento());

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

        user.show();

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

    }

}

package RJSJ.test20;

 

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 RJSJ.test20;

 

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

        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编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示