酒類網(wǎng)站建設(shè)方案百度推廣客戶端手機(jī)版
Unity實(shí)現(xiàn)設(shè)計模式——模板方法模式
模板模式(Template Pattern), 指在一個抽象類公開定義了執(zhí)行它的方法的模板。它的子類可以按需要重寫方法實(shí)現(xiàn),但調(diào)用將以抽象類中定義的方式進(jìn)行。
簡單說, 模板方法模式定義一個操作中的算法的骨架,而將這些步驟延遲到子類中,使得子類可以不改變一個算法的結(jié)構(gòu),就可以重定義該算法的某些特定步驟。
注意模板方法模式和策略模式的區(qū)別
模板模式注意強(qiáng)調(diào)了抽象類公開定義了一個執(zhí)行的模板方法,而策略模式是對單個算法的封裝更具有獨(dú)立性
下面使用兩個例子去介紹模板方法模式:
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();}}