首页 > 其他分享 >设计模式实验 20:备忘录模式

设计模式实验 20:备忘录模式

时间:2023-11-25 19:57:27浏览次数:31  
标签:account 设计模式 20 String 备忘录 telNo user password public

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

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

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

 
   

 


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

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

实验要求:

1.     提交源代码;

2.    package test20;
class Memento
{
    private String account;
    private String password;
    private String telNo;
    public Memento()
    {

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

}


package test20;

import java.util.ArrayList;

public class Caretaker
{
    private Memento memento;

    private ArrayList mementolist = new ArrayList();
    public Memento getMemento(int i) {
        return (Memento)mementolist.get(i);
    }
    public void setMemento(Memento memento) {
        mementolist.add(memento);
    }
}

package test20;

public class User
{
    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);
    }
}

package test20;

public class Client
{
    public static void main(String a[])
    {
        User user=new User();
        Caretaker c=new Caretaker();
        int index=0;
        user.setAccount("20213719");
        user.setPassword("123456");
        user.setTelNo("18700001542");
        System.out.println("状态一:");

        user.show();
        c.setMemento(user.saveMemento());//保存备忘录
        System.out.println("---------------------------");
        index++;
        user.setPassword("152987");
        user.setTelNo("18833152202");
        System.out.println("状态二:");
        user.show();
        c.setMemento(user.saveMemento());//保存备忘录
        System.out.println("---------------------------");
        index++;

        user.setPassword("564872");
        user.setTelNo("15522053367");
        System.out.println("状态三:");
        user.show();

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

        for(int i=index-1;i>=0;i--)
        {
            user.restoreMemento(c.getMemento(i));//从备忘录中恢复
            System.out.println("回到状态:"+(i+1));
            user.show();
            System.out.println("---------------------------");
        }
    }
}

 

标签:account,设计模式,20,String,备忘录,telNo,user,password,public
From: https://www.cnblogs.com/syhxx/p/17855967.html

相关文章

  • 87th 2023/11/25 一次反思
    打完NOIP后,感觉迷茫了一段时间因为自己真的挺菜的,去年没进复赛,还没什么感觉,一度的自我感觉良好今年去打了,很难受,CSP-S就因为一头扎在T2上,导致T3的大模拟没有去碰然后NOIP又玩了一次T2,其实这题是真的能切,思路也挺简单,但赛时没有想到,对这种套路并没有很熟悉但是想去打80分也没有......
  • 2023-11-25:用go语言,给定一个数组arr,长度为n,表示n个格子的分数,并且这些格子首尾相连, 孩
    2023-11-25:用go语言,给定一个数组arr,长度为n,表示n个格子的分数,并且这些格子首尾相连,孩子不能选相邻的格子,不能回头选,不能选超过一圈,但是孩子可以决定从任何位置开始选,也可以什么都不选。返回孩子能获得的最大分值。1<=n<=10^6,0<=arr[i]<=10^6。来自华为od。来自左程云。答案......
  • 求2/1,3/2,5/3...前20项之和
    #include<stdio.h>#include<math.h> intmain(){ floata=1,b=2,n,i,m,sum=0; for(i=1;i<=20;i++) {    n=b/a;  m=a+b;  a=b;  b=m;   sum+=n;  printf("%f%f%f%f%f\n",a,b,n,m,sum);   }  ......
  • 86th 2023/11/18 NOIP Day1
    已经过去了,总结得写赛前没什么,直接入题T1一眼了,T2看了看,手模了一下,觉得非常麻烦,难以处理T3看一眼认为不太能做,后来还剩0.5h时开了它,发现可以拿分T4看出了暴力,发现有一当应该是DP的部分分然后去推T2,然后很自信地认为,按它特殊数据给的数量,可以拿80分然后20min切了T1后,开始码T2......
  • 85th 2023/11/17 NOIP Day0
    明天就要在楼下的考场打了老师今天给了一整天的自主做题时间,初中生是做题,而高中就是复习了话不多说直接来到今天的归纳:斜率优化,要敢于把i,j移动至转移方程的左右,最后归纳为一次函数的形式,并观察需要去最值的东西,凸包似乎挺好维护关于区间:包含,相交,不相交,三种要考虑全,如:[P5464......
  • 84th 2023/11/16 NOIP Day-1
    一场模拟赛,下去试机了T1有正解思路,但思路混乱打不出来,主要是最后输出不是很懂如何处理T3没能完全想出正解,这个去重的思路挺有意思的主要是通过排序,预处理找到下一个重复位置,然后区间赋值来处理在一个位置即将重复时删除上一个即可T2T4待补......
  • 83rd 2023/11/15 NOIP Day-2
    早上回学校参加国标了,晚上继续停课训练思考了今天上午其他学校人打的模拟赛,T3是很有意思的网络流建图T1是一道贪心策略题,思路认真推之下应该能够想出老师讲了面对比赛应有的态度,是的,应该全力以赴面对这场难得的机会再补一下短板吧,DP、贪心和网络流建图(虽说不一定用得上),但万无......
  • 2023-2024-1 20232311 《网络空间安全导论》第3周学习总结
    2023-2024-120232311《网络空间安全导论》第3周学习教材内容学习总结网络空间安全导论第三章思维导图教材学习中的问题和解决过程问题1:不理解IP数据包结构问题1解决方案:询问chatgpt,令chatgpt举出了具体的示例以辅助理解问题2:不理解防火墙的具体原理问题2解决方案:查找了......
  • NOIP 2023比赛报告
    第一题比赛情况$100$分,耗时$1$小时。题解对于$1\lei\len$,比较$w_i$字典序最小的字符$a_i$与每个$w_j(i\nej)$字典序最大的字符$b_j$。如果有$b_j\lea_i$,则$w_i$不能成为字典序最小的单词,反之可以。代码第二题比赛情况$100$分,耗时$2$小时。题解......
  • 2023-11-25:用go语言,给定一个数组arr,长度为n,表示n个格子的分数,并且这些格子首尾相连, 孩
    2023-11-25:用go语言,给定一个数组arr,长度为n,表示n个格子的分数,并且这些格子首尾相连,孩子不能选相邻的格子,不能回头选,不能选超过一圈,但是孩子可以决定从任何位置开始选,也可以什么都不选。返回孩子能获得的最大分值。1<=n<=10^6,0<=arr[i]<=10^6。来自华为od。来自左程......