zoukankan      html  css  js  c++  java
  • 软件设计备忘录模式

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

    package 实验20备忘录模式;
    
    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);
            }
    }
    Caretaker
    package 实验20备忘录模式;
    
    
    
    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;
        }
       
    }
    Memento
    package 实验20备忘录模式;
    
    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);        
        }
    }
    UserInfoDTO
    package 实验20备忘录模式;
    
    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("---------------------------");
        }
        }
    }
    Client
  • 相关阅读:
    Access 通用访问类 OleDbHelper
    让Visual Studio 也支持JS代码折叠 [ Visual Studio | #region | #endregion ]
    提高网站性能的方法
    php 正则表达式整理 归纳 重点
    C++数据结构知识点
    algorithm算法设计,数据结构基本概念之我的归纳 by whb_咸菜圣斗士啊斌斌斌斌
    浏览器兼容性问题及常见的解决方法
    js抽象方法的使用
    js制作图片轮换切换
    C语言排序算法总结
  • 原文地址:https://www.cnblogs.com/feng747/p/15566368.html
Copyright © 2011-2022 走看看