北辰

c/c++/java程序

java设计模式进阶_memento

这里写图片描述

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : StarMemento.java
//  @ Date : 2016/8/30
//  @ Author : 
//
//




public interface StarMemento {
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : StarMementoInternal.java
//  @ Date : 2016/8/30
//  @ Author : 
//
//




public class StarMementoInternal implements StarMemento {

    private StarType type;
    private int ageYears;
    private int massTons;

    public StarType getType() {
        return type;
    }

    public void setType(StarType type) {
        this.type = type;
    }

    public int getAgeYears() {
        return ageYears;
    }

    public void setAgeYears(int year) {
        this.ageYears = year;
    }

    public int getMassTons() {
        return massTons;
    }

    public void setMassTons(int massTons) {
        this.massTons = massTons;
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Star.java
//  @ Date : 2016/8/30
//  @ Author : 
//
//




public class Star {
    private StarType type;
    private int ageYears;
    private int massTons;

    public Star(StarType type, int startAge, int startMass) {
        this.type = type;
        this.ageYears = startAge;
        this.massTons = startMass;
    }

    public void timePasses() {
        ageYears *= 2;
        massTons *= 8;
        switch(type)
        {
        case RED_GIANT:
            type = StarType.WHITE_DWARF;
            break;
        case SUN:
            type = StarType.RED_GIANT;
            break;
        case SUPERNOVA:
            type = StarType.DEAD;
            break;
        case WHITE_DWARF:
            type = StarType.SUPERNOVA;
            break;
        case DEAD:
            ageYears *= 2;
            massTons = 0;
            break;
        default:
            break;
        }
    }

    public StarMemento getMemento() {
        StarMementoInternal state = new StarMementoInternal();
        state.setAgeYears(ageYears);
        state.setMassTons(massTons);
        state.setType(type);
        return state;
    }

    public void setMemento(StarMemento memento) {
        StarMementoInternal state = (StarMementoInternal)memento;
        this.type = state.getType();
        this.ageYears = state.getAgeYears();
        this.massTons = state.getMassTons();
    }

    public String toString() {
        return String.format("%s age: %d years mass: %d tons", type.toString(),ageYears,massTons);
    }
}

public enum StarType {

    SUN("sun"),//太阳
    RED_GIANT("red giant"),//巨人
    WHITE_DWARF("white dwarf"),//白色-矮人
    SUPERNOVA("supernova"),//超新星
    DEAD("dead star"),//废星
    UNDEFINED("")//未命名
    ;
    private String title;

    StarType(String title){
        this.title = title;
    }

    public String toString()
    {
        return title;
    }

}


import java.util.Stack;

public class App {

    public static void main(String[] args) {
        Stack<StarMemento> states = new Stack<>();

        Star star = new Star(StarType.SUN,10000000,500000);
        System.out.println(star);
        states.add(star.getMemento());
        star.timePasses();
        System.out.println(star);
        states.add(star.getMemento());
        star.timePasses();
        System.out.println(star);
        states.add(star.getMemento());
        star.timePasses();
        System.out.println(star);
        states.add(star.getMemento());
        star.timePasses();
        System.out.println(star);
        while(states.size() > 0)
        {
            star.setMemento(states.pop());
            System.out.println(star);
        }
    }

}

/*sun age: 10000000 years mass: 500000 tons
red giant age: 20000000 years mass: 4000000 tons
white dwarf age: 40000000 years mass: 32000000 tons
supernova age: 80000000 years mass: 256000000 tons
dead star age: 160000000 years mass: 2048000000 tons
supernova age: 80000000 years mass: 256000000 tons
white dwarf age: 40000000 years mass: 32000000 tons
red giant age: 20000000 years mass: 4000000 tons
sun age: 10000000 years mass: 500000 tons
*/







阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwww_com/article/details/52369147
文章标签: java设计模式
个人分类: java设计模式进阶
上一篇java设计模式进阶_template-method
下一篇java设计模式进阶_state
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭