建造者模式
保卫萝卜中使用了建造者模式。UML图如下:

接口:
public interface IBuilder<T>
{
    //获取到游戏物体身上的脚本对象,从而去赋值
    T GetProductorClass(GameObject gameObject);
    //使用工厂去获取具体的游戏对象
    GameObject GetProduct();
    //获取数据信息
    void GetData(T productClassGo);
    //获取特有资源与信息
    void GetOtherResource(T productClassGo);
} 
建造怪兽类
using UnityEngine;
public class MonsterBuilder : IBuilder<Monster>
{
    public int m_monsterID;
    private GameObject monsterGo;
    public void GetData(Monster productClassGo)
    {
        productClassGo.monsterID = m_monsterID;
        productClassGo.HP = m_monsterID * 100;
        productClassGo.currentHP = productClassGo.HP;
        productClassGo.moveSpeed = m_monsterID > 7 ? 7:m_monsterID;  //速度太快了。子弹根本追不上
        productClassGo.initMoveSpeed = m_monsterID > 7 ? 7 : m_monsterID;
        productClassGo.prize = m_monsterID * 50;
    }
    public void GetOtherResource(Monster productClassGo)
    {
        productClassGo.GetMonsterProperty();
    }
    public GameObject GetProduct()
    {
        GameObject itemGo = GameController.Instance.GetGameObjectResource("MonsterPrefab");
        Monster monster = GetProductorClass(itemGo);
        GetData(monster);
        GetOtherResource(monster);
        return itemGo;
    }
    public Monster GetProductorClass(GameObject gameObject)
    {
        return gameObject.GetComponent<Monster>();
    }
}
 
塔的建造者
using UnityEngine;
/// <summary>
/// 塔的建造者
/// </summary>
public class TowerBuilder : IBuilder<Tower>
{
    public int m_towerID;
    private GameObject towerGO;
    public int m_towerLevel;  //塔的等级
    public void GetData(Tower productClassGo)
    {
        productClassGo.towerID = m_towerID;
    }
    public void GetOtherResource(Tower productClassGo)
    {
        productClassGo.GetTowerProperty();
    }
    public GameObject GetProduct()
    {
        GameObject gameObject = GameController.Instance.GetGameObjectResource
            ("Tower/ID" + m_towerID.ToString() + "/TowerSet/" + m_towerLevel.ToString());
        Tower tower = GetProductorClass(gameObject);
        GetData(tower);
        GetOtherResource(tower);
        return gameObject;
    }
    public Tower GetProductorClass(GameObject gameObject)
    {
        return gameObject.GetComponent<Tower>();
    }
}
 
建造者模式与工厂模式的区别:
建造者设计模式(Builder Design Pattern)和工厂设计模式(Factory Design Pattern)都是面向对象设计中的创建型模式,但它们解决的问题和应用场景有所不同。
- 在工厂方法中主要看中产品的整体创建,一般不考虑创建的各个部分细节;
 - 建造者模式一般用于对复杂产品的创建,可以进行分步骤详细创建;
 
建造者模式:
主要组件:
- Director(指挥者):负责使用构造器接口来构建一个复杂对象。
 - Builder(构造器):定义对象的构建过程,包括设置属性、添加部件等方法。
 - ConcreteBuilder(具体构造器):实现构造器接口,实现具体的构建方法。
 - Product(产品):最终构建出的复杂对象。
 
建造者模式的优点是将对象的构建过程封装,使得代码更加清晰,同时能够灵活地构建不同的对象。
工厂模式:
工厂设计模式旨在通过一个工厂来创建对象,将对象的创建过程封装起来,客户端代码无需直接调用构造函数。它分为简单工厂、工厂方法和抽象工厂等形式。
-  
主要组件:
- Factory(工厂):负责创建对象的接口或类。
 - ConcreteFactory(具体工厂):实现工厂接口,实际创建对象的地方。
 - Product(产品):工厂创建的对象。
 
 
工厂模式的主要优点是将对象的创建和客户端解耦,客户端只需通过工厂来获取对象,不需要关心对象的具体创建过程。
目的不同:
- 建造者模式关注于创建复杂对象的构建过程,将构建过程和表示分离。
 - 工厂模式关注于对象的创建,将对象的创建过程封装在工厂中,以便在客户端中使用。
 
复杂性:
- 建造者模式通常用于创建复杂对象,因为对象的构建过程可能涉及多个步骤和配置选项。
 - 工厂模式可以用于创建不同类型的对象,包括简单对象和复杂对象。
 
关注点:
- 建造者模式更关注于对象的构建过程,尤其适合需要按照一定步骤构建对象的情况。
 - 工厂模式更关注于对象的创建,强调封装创建过程,便于对象创建的管理。
 
综上所述,建造者模式适用于构建复杂对象,而工厂模式适用于创建对象的封装和管理。选择适当的模式取决于你的设计需求和对象创建的复杂性。
资源工厂模式:
资源工厂接口
/// <summary>
/// 其他种类资源工厂的接口,每种工程获取的资源都不同
/// </summary>
public interface IBaseResourceFactory<T>
{
    T GetSingleResources(string resourcePath);
} 
音频资源工厂
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 音频资源工厂
/// </summary>
public class AudioClipFactory : IBaseResourceFactory<AudioClip>
{
    //资源字典
    protected Dictionary<string, AudioClip> factoryDict = new Dictionary<string, AudioClip>();
    protected string loadPath;
    public AudioClipFactory()
    {
        loadPath = "AudioClips/";
    }
    public AudioClip GetSingleResources(string resourcePath)
    {
        AudioClip itemGo = null;
        string itemLoadPath = loadPath + resourcePath;
        if (factoryDict.ContainsKey(resourcePath))
        {
            itemGo = factoryDict[resourcePath];
        }
        else
        {
            itemGo = Resources.Load<AudioClip>(itemLoadPath);
            factoryDict.Add(resourcePath, itemGo);
        }
        if (itemGo == null)
        {
            Debug.Log(resourcePath + "的资源获取失败,失败路径为:" + itemLoadPath);
        }
        return itemGo;
    }
}
 
protected Dictionary<string, RuntimeAnimatorController> factoryDict = new Dictionary<string, RuntimeAnimatorController>();
获取的资源可以用一个字典存起来方便之后继续使用



















