zoukankan      html  css  js  c++  java
  • MementoPattern(备忘录模式)

    /**
     * 备忘录模式
     * @author TMAC-J
     * 用于存储bean的状态
     */
    public class MementoPattern {
        
        public class Memento{
            
            private int lifeValue;
            
            private int grade;
    
            public int getLifeValue() {
                return lifeValue;
            }
    
            public void setLifeValue(int lifeValue) {
                this.lifeValue = lifeValue;
            }
    
            public int getGrade() {
                return grade;
            }
    
            public void setGrade(int grade) {
                this.grade = grade;
            }
            
        }
        
        public class Originator{
            
            public Originator(int lifeValue,int grade) {
                this.grade = grade;
                this.lifeValue = lifeValue;
            }
            
            private int lifeValue;
            
            private int grade;
    
            public int getLifeValue() {
                return lifeValue;
            }
    
            public void setLifeValue(int lifeValue) {
                this.lifeValue = lifeValue;
            }
    
            public int getGrade() {
                return grade;
            }
    
            public void setGrade(int grade) {
                this.grade = grade;
            }
            
            public Memento createMemento(){
                Memento memento = new Memento();
                memento.setGrade(this.grade);
                memento.setLifeValue(this.lifeValue);
                return memento;
            }
            
            public void setMemento(Memento memento){
                this.lifeValue = memento.getLifeValue();
                this.grade = memento.getGrade();
            }
            
        }
        
        public class MementoManage{
            
            private Memento memento;
    
            public Memento getMemento() {
                return memento;
            }
    
            public void setMemento(Memento memento) {
                this.memento = memento;
            }
            
        }
        
        public void test(){
            Originator originator = new Originator(10,10);
            Memento memento = originator.createMemento();
            originator.setMemento(memento);
        }
        
    }
  • 相关阅读:
    image对象
    Frame/IFrame 对象
    Form 对象
    JavaScript 对象 实例
    button对象
    正则介绍以及多种使用方法
    js /jquery停止事件冒泡和阻止浏览器默认事件
    一些兼容性的知识
    面试题总结
    事件
  • 原文地址:https://www.cnblogs.com/yzjT-mac/p/6233725.html
Copyright © 2011-2022 走看看