unity中的模板方法模式

using UnityEngine;

public class TempleMethod : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        IPeaple peaple = new SouthPeaple();
        peaple.GORestaurant();
        peaple = new NorthPeaple();
        peaple.GORestaurant();
    }
}

public abstract class IPeaple
{
    public void GORestaurant()
    {
        OrderAMeal();
        Eat();
        PayTheBill();
    }

    protected abstract void Eat();

    protected void OrderAMeal()
    {
        Debug.Log("點單");
    }

    protected void PayTheBill()
    {
        Debug.Log("買單");
    }
}

public class SouthPeaple : IPeaple
{
    protected override void Eat()
    {
        Debug.Log("吃米飯");
    }
}

public class NorthPeaple : IPeaple
{
    protected override void Eat()
    {
        Debug.Log("吃麪");
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章