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);
    }
    
}

 

posted @ 2016-12-29 16:58  麦子TMAC  阅读(285)  评论(0编辑  收藏  举报