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);
        }
        
    }
  • 相关阅读:
    微信小程序 数据绑定方式
    wxss与rpx
    Ubuntu 编译安装 Xdebug
    PHP运算符优先级
    有了art-template,如有神助
    laydate控制之前的日期不可选择
    label和span的区别
    phpredis基本操作
    FILE,id不一致
    双层保障,年龄的输入
  • 原文地址:https://www.cnblogs.com/yzjT-mac/p/6233725.html
Copyright © 2011-2022 走看看