Template模板模式_C#

本文介绍了一个使用模板方法模式的Unity游戏开发示例,通过抽象基类定义汉堡制作流程,子类实现具体步骤,展示了OmnivoreHoagie和VeggieHoagie两种汉堡的定制化制作过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


using UnityEngine;
using System.Collections;

namespace TemplateMethodPatternExample1
{

    public class TemplateMethodPatternExample1 : MonoBehaviour
    {
        void Start()
        {
            Hoagie cust12Hoagie = new OmnivoreHoagie();
            cust12Hoagie.Makehamburger();

            Hoagie cust13Hoagie = new VeggieHoagie();
            cust13Hoagie.Makehamburger();
        }
    }

    public abstract class Hoagie
    {
        public void Makehamburger()
        {
            Debug.Log("做一个汉堡包");

            CutBun();

            if (CustomerWantsMeat())
            {
                AddMeat();
            }

            if (CustomerWantsCheese())
            {
                AddCheese();
            }

            if (CustomerWantsVegetables())
            {
                AddVegetables();
            }

            if (CustomerWantsCondiments())
            {
                AddCondiments();
            }

            WrapTheHoagie();
        }
        protected abstract void AddMeat();
        protected abstract void AddCheese();
        protected abstract void AddVegetables();
        protected abstract void AddCondiments();

        protected virtual bool CustomerWantsMeat() { return true; } // 告诉厨师要不要做
        protected virtual bool CustomerWantsCheese() { return true; }
        protected virtual bool CustomerWantsVegetables() { return true; }
        protected virtual bool CustomerWantsCondiments() { return true; }

        protected void CutBun()
        {
            Debug.Log("面包切好了");
        }

        protected void WrapTheHoagie()
        {
            Debug.Log("打包好了");
        }
    }

    //啥都吃
    public class OmnivoreHoagie : Hoagie
    {
        protected override void AddMeat()
        {
            Debug.Log("加肉");
        }

        protected override void AddCheese()
        {
            Debug.Log("加芝士");
        }

        protected override void AddVegetables()
        {
            Debug.Log("加生菜");
        }

        protected override void AddCondiments()
        {
            Debug.Log("加沙拉");
        }
    }


    //素食主义
    public class VeggieHoagie : Hoagie
    {
        protected override void AddMeat()
        {
        }

        protected override void AddCheese()
        {
        }

        protected override void AddVegetables()
        {
            Debug.Log("加生菜");
        }

        protected override void AddCondiments()
        {
            Debug.Log("加沙拉");
        }
        //告诉厨师不需要做肉和芝士了
        protected override bool CustomerWantsMeat() { return false; }
        protected override bool CustomerWantsCheese() { return false; }

    }


 

其实就是子类重写父类的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值