设计模式---工厂模式(DesignPattern_FactoryMethod)

摘录自:设计模式与游戏完美开发

十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、Product Evangelist at Unity Technologies——Kelvin Lo、信仁软件设计创办人—— 赖信仁、资深3D游戏美术——刘明恺 联合推荐全书采用了整合式的项目教学,即以一个游戏的范例来应用23种设计模式的实现贯穿全书,让读者学习到整个游戏开发的全过程和作者想要传承的经验,并以浅显易懂的比喻来解析难以理解的设计模式,让想深入了解此领域的读者更加容易上手。


工程GitHub

FACTORY METHOD—请MM去麦当劳吃汉堡,不同的MM有不同的口味,要每个都记住是一件烦人的事情,我一般采用Factory Method模式,带着MM到服务员那儿,说“要一个汉堡”,具体要什么样的汉堡呢,让MM直接跟服务员说就行了。

工厂方法模式:核心工厂类不再负责所有产品的创建,而是将具体创建的工作交给子类去做,成为一个抽象工厂角色,仅负责给出具体工厂类必须实现的接口,而不接触哪一个产品类应当被实例化这种细节。

using UnityEngine;
using System.Collections;

namespace DesignPattern_FactoryMethod
{
    // 产品抽象类
    public abstract class Product
    {
    }

    // 产品A
    public class ConcreteProductA : Product
    {
        public ConcreteProductA()
        {
            Debug.Log("生成物件類型A");
        }
    }

    // 产品B
    public class ConcreteProductB : Product
    {
        public ConcreteProductB()
        {
            Debug.Log("生成物件類型B");
        }
    }

    // 通知factory生产产品 , factory对应的子类会传回需要的产品类别
    public abstract class Creator
    {
        public abstract Product FactoryMethod();
    }

    // 生产ProductA的工厂 
    public class ConcreteCreatorProductA : Creator
    {
        public ConcreteCreatorProductA()
        {
            Debug.Log("產生工廠:ConcreteCreatorProductA");
        }

        public override Product FactoryMethod()
        {
            return new ConcreteProductA();
        }
    }

    // 生产ProductB的工厂
    public class ConcreteCreatorProductB : Creator
    {
        public ConcreteCreatorProductB()
        {
            Debug.Log("產生工廠:ConcreteCreatorProductB");
        }
        public override Product FactoryMethod()
        {
            return new ConcreteProductB();
        }
    }

    // 通知factory method,会依照参数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("產生工廠:ConcreteCreator_MethodType");
        }

        public override Product FactoryMethod(int Type)
        {
            switch (Type)
            {
                case 1:
                    return new ConcreteProductA();
                case 2:
                    return new ConcreteProductB();
            }
            Debug.Log("Type[" + Type + "]無法產生物件");
            return null;
        }
    }

    // 声明接口及factory method,并限定接口的使用方式
    interface Creator_GenericMethod
    {
        Product FactoryMethod() where T : Product, new();
    }

    // 重写factory method,返回需要的Product
    public class ConcreteCreator_GenericMethod : Creator_GenericMethod
    {
        public ConcreteCreator_GenericMethod()
        {
            Debug.Log("產生工廠:ConcreteCreator_GenericMethod");
        }

        public Product FactoryMethod() where T : Product, new()
        {
            return new T();
        }
    }

    // 声明Generic factory类別
    public class Creator_GenericClass where T : Product, new()
    {
        public Creator_GenericClass()
        {
            Debug.Log("產生工廠: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()
    {

        // 产品
        Product theProduct = null;

        // 工厂抽象类
        Creator theCreator = null;

        // 设定为生产A的工厂
        theCreator = new ConcreteCreatorProductA();
        theProduct = theCreator.FactoryMethod();

        // 设定为生产B的工厂
        theCreator = new ConcreteCreatorProductB();
        theProduct = theCreator.FactoryMethod();

        // 工厂抽象基类
        Creator_MethodType theCreatorMethodType = new ConcreteCreator_MethodType();

        // 输入不同的类型产生不同的产品
        theProduct = theCreatorMethodType.FactoryMethod(1);
        theProduct = theCreatorMethodType.FactoryMethod(2);

        // 使用Generic Method
        ConcreteCreator_GenericMethod theCreatorGM = new ConcreteCreator_GenericMethod();
        theProduct = theCreatorGM.FactoryMethod();
        theProduct = theCreatorGM.FactoryMethod();

        // 使用Generic Class
        // 负责ProduceA的工廠
        Creator_GenericClass Creator_ProductA = new Creator_GenericClass();
        theProduct = Creator_ProductA.FactoryMethod();

        // 负责ProduceA的工廠
        Creator_GenericClass Creator_ProductB = new Creator_GenericClass();
        theProduct = Creator_ProductB.FactoryMethod();
    }
}

你可能感兴趣的:(设计模式---工厂模式(DesignPattern_FactoryMethod))