首页 > 代码库 > 审判,必须死,关于工厂模式的小小理解

审判,必须死,关于工厂模式的小小理解

不幸,你被抓住了。

明朝皇帝不会留情的。他冷笑着说:“你必须死!!!”

“交给东厂,审审,决定你的死法”

这个世道啊,连死法,都和你有多少钱相关。

“身上钱少于100块,吃屎到死;多于100,少于200,被袋鼠干,干死;多余200块嘛,天天看郭敬明,郁闷至死。”

于是一段程序开始了:

using UnityEngine;
using System.Collections;

public class DummyFact : MonoBehaviour {
    //king want you to die
    //you are a criminal
    //many ways to die
    //depends on your money
    //money<100  ------>eat shit to die
    //100<=money <200  ------>fucked by kangaroo to die
    //mony>=200        ------>read novels by GuoJingMing to die
    // Use this for initialization
    void Start () {
        Criminal you = new Criminal ();
        you.ToDie ();
        you.Money = 99;
        Debug.Log ("your money ="+you.Money);
        you = SentenceFactory.Sentence (you);
        you.ToDie ();
    }
    
    // Update is called once per frame
    void Update () {
    
    }

}

public class Criminal{
    private int _money;
    public int Money {
                get;
                set;
    }
    public virtual void ToDie(){
        Debug.Log ("You should die");
    }
}

public class PoorCriminal:Criminal{
    public override void ToDie(){
        Debug.Log ("eat shit to die");
    }
}

public class MiddleCriminal:Criminal{
    public override void ToDie(){
        Debug.Log ("fucked by kangaroo to die");
    }
}

public class RichCriminal:Criminal{
    public override void ToDie(){
        Debug.Log ("read novels by GuoJingMing to die");
    }
}

public class SentenceFactory{
    public static Criminal Sentence(Criminal you){
        Criminal youSentenced = null;
        int money = you.Money;
        if(money<100){
            youSentenced = new PoorCriminal();
        }
        else if(money>=100&&money<200){
            youSentenced = new MiddleCriminal();
        }
        else{
            youSentenced = new RichCriminal();
        }
        return youSentenced;
    }
}