关闭

java设计模式进阶_memento

标签: java设计模式
1237人阅读 评论(0) 收藏 举报
分类:

这里写图片描述

//
//
//  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
*/







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:55393次
    • 积分:1626
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:42篇
    • 译文:42篇
    • 评论:8条