当前位置: 首页 > news >正文

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("");}
}

(二)ConcreteClassA

class ConcreteClassA : AbstractClass
{public override void PrimitiveOperation1(){Debug.Log("ConcreteClassA.PrimitiveOperation1()");}public override void PrimitiveOperation2(){Debug.Log("ConcreteClassA.PrimitiveOperation2()");}
}

(三)ConcreteClassB

class ConcreteClassB : AbstractClass
{public override void PrimitiveOperation1(){Debug.Log("ConcreteClassB.PrimitiveOperation1()");}public override void PrimitiveOperation2(){Debug.Log("ConcreteClassB.PrimitiveOperation2()");}
}

(四)测试

public class TemplateMethodStructure : MonoBehaviour
{void Start ( ){AbstractClass aA = new ConcreteClassA();aA.TemplateMethod();AbstractClass aB = new ConcreteClassB();aB.TemplateMethod();}
}

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 Hookprotected 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.");}}

(二)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");}}

(三)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; }}

(四)错误的方式

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");}}}

(五)测试

    public class TemplateMethodPatternExample1 : MonoBehaviour{void Start(){Hoagie cust12Hoagie = new ItalienHoagie();cust12Hoagie.MakeSandwich();Hoagie cust13Hoagie = new VeggieHoagie();cust13Hoagie.MakeSandwich();}}
http://www.lryc.cn/news/187779.html

相关文章:

  • C++实现高性能内存池(二)
  • 沪深300期权一个点多少钱?
  • 怎么防止重要文件夹丢失?文件夹安全如何保护?
  • 用于物体识别和跟踪的下游任务自监督学习-1-引言
  • 式子表达ds类——多用位置/值域表示未知数+区间覆盖转区间加:CF407E
  • Python 实现秒表功能(比较好玩的题目)
  • DALL-E 3调参教程;百度新出的AI写小说神器;通义听悟看播客也太爽了;系列博文带你理解生成式AI | ShowMeAI日报
  • 设计模式-享元模式
  • 中秋时节赏明月,五子棋戏月饼趣 — Flutter中秋限定版五子棋
  • Scala第十九章节
  • kafka与hbase的区别
  • 出栈序列的合法性
  • unity操作_刚体 c#
  • 网络编程中套接字(socket)介绍(Python示例)
  • d3dcompiler_43.dll是什么文件?缺失d3dcompiler_43.dll文件修复与解决方法
  • YOLOv7改进:SPD-Conv,低分辨率图像和小物体涨点明显,涨点神器!!!
  • iris(golang)连接mysql数据库
  • C现代方法(第1、2章)笔记
  • 练[CISCN2019 华东南赛区]Double Secret
  • 『Linux - gcc / g++』c程序翻译过程
  • 苹果遭遇安全危机,应用商店曝出不良APP,或影响iPhone的销售
  • docker 基本操作
  • ARM:使用汇编完成三个灯流水亮灭
  • 嵌入式养成计划-33--数据库-sqlite3
  • 什么是大数据运维?大数据运维的职责
  • 解决方案:AI赋能工业生产3.0,从工业“制造”到“智造”
  • Android KeyStore 秘钥导入
  • TDengine+OpenVINO+AIxBoard,助力时序数据分类
  • 设计模式——16. 迭代器模式
  • flink redis connector需要防止包冲突