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);
        }
        
    }
  • 相关阅读:
    MapGuide 资源
    mac 安装brew
    mysql 设置外键约束SET FOREIGN_KEY_CHECKS=1
    一文深入讲解redis和couchbase的区别
    mac 显示 sh3.2# 的
    DBA整理的万字详解MySQL性能优化,值得收藏!
    redis和couchbase的比较
    && 运算符的使用
    freeotp 安装及使用过程
    Couchbase vs Redis,究竟哪个更胜一筹?
  • 原文地址:https://www.cnblogs.com/yzjT-mac/p/6233725.html
Copyright © 2011-2022 走看看