摘錄自:設(shè)計模式與游戲完美開發(fā)
十年磨一劍,作者將設(shè)計模式理論巧妙地融入到實踐中,以一個游戲的完整實現(xiàn)呈現(xiàn)設(shè)計模式的應用及經(jīng)驗的傳承 《軒轅劍》之父——蔡明宏、資深游戲制作人——李佳澤、Product Evangelist at Unity Technologies——Kelvin Lo、信仁軟件設(shè)計創(chuàng)辦人—— 賴信仁、資深3D游戲美術(shù)——劉明愷 聯(lián)合推薦全書采用了整合式的項目教學,即以一個游戲的范例來應用23種設(shè)計模式的實現(xiàn)貫穿全書,讓讀者學習到整個游戲開發(fā)的全過程和作者想要傳承的經(jīng)驗,并以淺顯易懂的比喻來解析難以理解的設(shè)計模式,讓想深入了解此領(lǐng)域的讀者更加容易上手。
工程GitHub
FACTORY METHOD—請MM去麥當勞吃漢堡,不同的MM有不同的口味,要每個都記住是一件煩人的事情,我一般采用Factory Method模式,帶著MM到服務員那兒,說“要一個漢堡”,具體要什么樣的漢堡呢,讓MM直接跟服務員說就行了。
工廠方法模式:核心工廠類不再負責所有產(chǎn)品的創(chuàng)建,而是將具體創(chuàng)建的工作交給子類去做,成為一個抽象工廠角色,僅負責給出具體工廠類必須實現(xiàn)的接口,而不接觸哪一個產(chǎn)品類應當被實例化這種細節(jié)。
using UnityEngine;
using System.Collections;
namespace DesignPattern_FactoryMethod
{
// 產(chǎn)品抽象類
public abstract class Product
{
}
// 產(chǎn)品A
public class ConcreteProductA : Product
{
public ConcreteProductA()
{
Debug.Log("生成物件類型A");
}
}
// 產(chǎn)品B
public class ConcreteProductB : Product
{
public ConcreteProductB()
{
Debug.Log("生成物件類型B");
}
}
// 通知factory生產(chǎn)產(chǎn)品 , factory對應的子類會傳回需要的產(chǎn)品類別
public abstract class Creator
{
public abstract Product FactoryMethod();
}
// 生產(chǎn)ProductA的工廠
public class ConcreteCreatorProductA : Creator
{
public ConcreteCreatorProductA()
{
Debug.Log("產(chǎn)生工廠:ConcreteCreatorProductA");
}
public override Product FactoryMethod()
{
return new ConcreteProductA();
}
}
// 生產(chǎn)ProductB的工廠
public class ConcreteCreatorProductB : Creator
{
public ConcreteCreatorProductB()
{
Debug.Log("產(chǎn)生工廠:ConcreteCreatorProductB");
}
public override Product FactoryMethod()
{
return new ConcreteProductB();
}
}
// 通知factory method,會依照參數(shù)Type的提示回返回相應Product
public abstract class Creator_MethodType
{
public abstract Product FactoryMethod(int Type);
}
// 重寫factory method,返回需要的Product
public class ConcreteCreator_MethodType : Creator_MethodType
{
public ConcreteCreator_MethodType()
{
Debug.Log("產(chǎn)生工廠:ConcreteCreator_MethodType");
}
public override Product FactoryMethod(int Type)
{
switch (Type)
{
case 1:
return new ConcreteProductA();
case 2:
return new ConcreteProductB();
}
Debug.Log("Type[" + Type + "]無法產(chǎn)生物件");
return null;
}
}
// 聲明接口及factory method,并限定接口的使用方式
interface Creator_GenericMethod
{
Product FactoryMethod<T>() where T : Product, new();
}
// 重寫factory method,返回需要的Product
public class ConcreteCreator_GenericMethod : Creator_GenericMethod
{
public ConcreteCreator_GenericMethod()
{
Debug.Log("產(chǎn)生工廠:ConcreteCreator_GenericMethod");
}
public Product FactoryMethod<T>() where T : Product, new()
{
return new T();
}
}
// 聲明Generic factory類別
public class Creator_GenericClass<T> where T : Product, new()
{
public Creator_GenericClass()
{
Debug.Log("產(chǎn)生工廠:Creator_GenericClass<" + typeof(T).ToString() + ">");
}
public Product FactoryMethod()
{
return new T();
}
}
}
using UnityEngine;
using System.Collections;
using DesignPattern_FactoryMethod;
public class FactoryMethodTest : MonoBehaviour
{
void Start()
{
UnitTest();
}
void UnitTest()
{
// 產(chǎn)品
Product theProduct = null;
// 工廠抽象類
Creator theCreator = null;
// 設(shè)定為生產(chǎn)A的工廠
theCreator = new ConcreteCreatorProductA();
theProduct = theCreator.FactoryMethod();
// 設(shè)定為生產(chǎn)B的工廠
theCreator = new ConcreteCreatorProductB();
theProduct = theCreator.FactoryMethod();
// 工廠抽象基類
Creator_MethodType theCreatorMethodType = new ConcreteCreator_MethodType();
// 輸入不同的類型產(chǎn)生不同的產(chǎn)品
theProduct = theCreatorMethodType.FactoryMethod(1);
theProduct = theCreatorMethodType.FactoryMethod(2);
// 使用Generic Method
ConcreteCreator_GenericMethod theCreatorGM = new ConcreteCreator_GenericMethod();
theProduct = theCreatorGM.FactoryMethod<ConcreteProductA>();
theProduct = theCreatorGM.FactoryMethod<ConcreteProductB>();
// 使用Generic Class
// 負責ProduceA的工廠
Creator_GenericClass<ConcreteProductA> Creator_ProductA = new Creator_GenericClass<ConcreteProductA>();
theProduct = Creator_ProductA.FactoryMethod();
// 負責ProduceA的工廠
Creator_GenericClass<ConcreteProductB> Creator_ProductB = new Creator_GenericClass<ConcreteProductB>();
theProduct = Creator_ProductB.FactoryMethod();
}
}