• 今日收获


    实验 20:备忘录模式

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

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

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

     

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

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

    package shiyan20;
    
    
    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 shiyan20;
    
    
    public class Client
    {
        public static void main(String a[])
        {
        UserInfoDTO user=new UserInfoDTO();
        Caretaker c=new Caretaker();
        int index=0;
        user.setAccount("zhangsan");
        user.setPassword("123456");
        user.setTelNo("13000000000");
        System.out.println("状态一:");
     
        user.show();
        c.setMemento(user.saveMemento());//保存备忘录
        System.out.println("---------------------------");
        index++;
        user.setPassword("111111");
        user.setTelNo("13100001111");
        System.out.println("状态二:");    
        user.show();
        c.setMemento(user.saveMemento());//保存备忘录
        System.out.println("---------------------------");
        index++;
        
        user.setPassword("555555");
        user.setTelNo("13100005555");
        System.out.println("状态三:");    
        user.show();
        
        System.out.println("---------------------------");
        
        for(int i=index-1;i>=0;i--)
        {
            int j=i+1;
        user.restoreMemento(c.getMemento(i));//从备忘录中恢复
        System.out.println("回到状态:"+j);
        user.show();
        System.out.println("---------------------------");
        }
        }
    }
    package shiyan20;
    
    
    
    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 shiyan20;
    
    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);        
        }
    }
  • 相关阅读:
    HTML<lable for="">标签的for属性。
    Microsoft_Office_Word_遇到问题需要关闭。我们对此引起的不便表示抱歉,问题解决方案
    AnyChart的资料,以后看
    JQquery 鼠标悬浮提示
    如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?
    JQuery UI selectable
    SqlServer 动态添加服务器
    基于CyberGarage库的dlna开发(android)
    自定义实现圆形播放进度条(android,飞一般的感觉)
    Lance老师UI系列教程第一课>QQ设置界面的实现(android)
  • 原文地址:https://www.cnblogs.com/Nojava/p/14157198.html
Copyright © 2020-2023  润新知