• Unity实现设计模式——模板方法模式


    Unity实现设计模式——模板方法模式

    模板模式(Template Pattern), 指在一个抽象类公开定义了执行它的方法的模板。它的子类可以按需要重写方法实现,但调用将以抽象类中定义的方式进行。
    简单说, 模板方法模式定义一个操作中的算法的骨架,而将这些步骤延迟到子类中,使得子类可以不改变一个算法的结构,就可以重定义该算法的某些特定步骤。

    在这里插入图片描述
    注意模板方法模式和策略模式的区别
    模板模式注意强调了抽象类公开定义了一个执行的模板方法,而策略模式是对单个算法的封装更具有独立性

    下面使用两个例子去介绍模板方法模式:

    1.第一个例子(使用比较抽象的例子)

    (一)AbstractClass

    abstract class AbstractClass
    {
        public abstract void PrimitiveOperation1();
        public abstract void PrimitiveOperation2();
    
        // The "Template method"
        public void TemplateMethod()
        {
            PrimitiveOperation1();
            PrimitiveOperation2();
            Debug.Log("");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    (二)ConcreteClassA

    class ConcreteClassA : AbstractClass
    {
        public override void PrimitiveOperation1()
        {
            Debug.Log("ConcreteClassA.PrimitiveOperation1()");
        }
        public override void PrimitiveOperation2()
        {
            Debug.Log("ConcreteClassA.PrimitiveOperation2()");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    (三)ConcreteClassB

    class ConcreteClassB : AbstractClass
    {
        public override void PrimitiveOperation1()
        {
            Debug.Log("ConcreteClassB.PrimitiveOperation1()");
        }
        public override void PrimitiveOperation2()
        {
            Debug.Log("ConcreteClassB.PrimitiveOperation2()");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    (四)测试

    public class TemplateMethodStructure : MonoBehaviour
    {
    	void Start ( )
    	{
            AbstractClass aA = new ConcreteClassA();
            aA.TemplateMethod();
    
            AbstractClass aB = new ConcreteClassB();
            aB.TemplateMethod();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.第二个例子(使用一个三明治的制作过程来介绍)

    (一)Hoagie 三明治抽象基类

        public abstract class Hoagie
        {
            public void MakeSandwich()
            {
                Debug.Log("Making new Sandwich");
    
                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; } // << called Hook
            protected virtual bool CustomerWantsCheese() { return true; }
            protected virtual bool CustomerWantsVegetables() { return true; }
            protected virtual bool CustomerWantsCondiments() { return true; }
    
            protected void CutBun()
            {
                Debug.Log("Bun is Cut");
            }
    
            protected void WrapTheHoagie()
            {
                Debug.Log("Hoagie is wrapped.");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    (二)ItalienHoagie 法式三明治

        public class ItalienHoagie : Hoagie
        {
            protected override void AddMeat()
            {
                Debug.Log("Adding the Meat: Salami");
            }
    
            protected override void AddCheese()
            {
                Debug.Log("Adding the Cheese: Provolone");
            }
    
            protected override void AddVegetables()
            {
                Debug.Log("Adding the Vegetables: Tomatoes");
            }
    
            protected override void AddCondiments()
            {
                Debug.Log("Adding the Condiments: Vinegar");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    (三)VeggieHoagie 素菜三明治

        public class VeggieHoagie : Hoagie
        {
            protected override void AddMeat()
            {
            }
    
            protected override void AddCheese()
            {
            }
    
            protected override void AddVegetables()
            {
                Debug.Log("Adding the Vegetables: Tomatoes");
            }
    
            protected override void AddCondiments()
            {
                Debug.Log("Adding the Condiments: Vinegar");
            }
    
            protected override bool CustomerWantsMeat() { return false; }
            protected override bool CustomerWantsCheese() { return false; }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    (四)错误的方式

    namespace BadExample
        {
            // this way you would have to rewrite a lot of code
            // especially if something changes or another class differs and does e.g. not AddMeat()
            public class ItalienHoagie
            {
                public void MakeSandwich()
                {
                    CutBun();
                    AddMeat();
                    AddCheese();
                    AddVegtables();
                    AddCondiments();
                    WrapHoagie();
                }
    
                public void CutBun()
                {
                    Debug.Log("Hoagie is Cut");
                }
    
                public void AddMeat()
                {
                    Debug.Log("Added Meat");
                }
    
                public void AddCheese()
                {
                    Debug.Log("Added Cheese");
                }
    
                public void AddVegtables()
                {
                    Debug.Log("Added Vegies");
                }
    
                public void AddCondiments()
                {
                    Debug.Log("Added Condiments");
                }
    
                public void WrapHoagie()
                {
                    Debug.Log("Wrapped Hoagie");
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    (五)测试

        public class TemplateMethodPatternExample1 : MonoBehaviour
        {
            void Start()
            {
                Hoagie cust12Hoagie = new ItalienHoagie();
                cust12Hoagie.MakeSandwich();
    
                Hoagie cust13Hoagie = new VeggieHoagie();
                cust13Hoagie.MakeSandwich();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    凸包P2742
    ajax 中 success 方法的 return
    数据结构 - 图
    记录:ubuntu安装zlog及使用
    ssm网上书城系统毕业设计-附源码180919
    EMANE中olsrd的调试
    navigation2 编写规划器(planner)插件
    第一节 vue3 router内置类型有哪些
    Peppol 详解指南
    广东省科学技术厅关于2023年度广东省科学技术奖提名工作的通知
  • 原文地址:https://blog.csdn.net/zzzsss123333/article/details/133682441