设计模式---工厂模式(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<T>() where T : Product, new();
    }

    // 重写factory method,返回需要的Product
    public class ConcreteCreator_GenericMethod : Creator_GenericMethod
    {
        public ConcreteCreator_GenericMethod()
        {
            Debug.Log("產生工廠: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("產生工廠: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<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();
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 好的软件设计是多用代码复用,但实际设计中却要遇到许多变化,而变化是复用的天敌。为了能够尽量减少变化所带来的设计复杂...
    CharlesW阅读 3,652评论 0 0
  • 转载自:经典:从追MM谈Java的23种设计模式 佩服原文作者对设计模式的深刻理解,哈哈哈😁 一. 创建型模式 1...
    涛大阅读 4,429评论 0 2
  • 构造型模式 Factory Method (工厂方法) Abstract Factory(抽象工厂) Builde...
    738bc070cd74阅读 3,021评论 1 4
  • 创建型模式 1、FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不...
    珈谊阅读 2,744评论 0 0
  • 肥胖的危害 一胖毁所有:青春,自信,爱情,婚姻,事业,前程,,, 肥胖不仅影响人的形象和工 作效率,而且还会带来一...
    菡丹阅读 2,350评论 0 0

友情链接更多精彩内容