借鉴于:
http://blog.csdn.net/jason0539/article/details/23020989
《设计模式Head First》
1、工厂模式例子原型
只介绍抽象工厂模式,更多可以看参考里大佬的博客。
假如我们需要宝马车,而且不同配置的宝马车需要不同的发动机和空调来组装成骑车。需要由组装后交给客户。
抽象工厂模式UML
抽象工厂模式原型代码
/// <summary>
/// 工厂模式
/// </summary>
public class FactoryMode : MonoBehaviour
{
private void Start()
{
CarFactory car200 = new CarBMW200();
car200.CreateEngine();
car200.CreateAircondition();
CarFactory car300 = new CarBMW300();
car300.CreateEngine();
car300.CreateAircondition();
}
}
/// <summary>
/// 汽车发动机
/// </summary>
public interface CarEngine
{
}
public class CarEngine1: CarEngine
{
public CarEngine1()
{
Debug.Log("制造发动机1型号。");
}
}
public class CarEngine2 : CarEngine
{
public CarEngine2()
{
Debug.Log("制造发动机2型号。");
}
}
/// <summary>
/// 汽车空调
/// </summary>
public interface CarAircondition
{
}
public class CarAircondition1 : CarAircondition
{
public CarAircondition1()
{
Debug.Log("制造空调1型号。");
}
}
public class CarAircondition2 : CarAircondition
{
public CarAircondition2()
{
Debug.Log("制造空调2型号。");
}
}
/// <summary>
/// 汽车工厂模式
/// </summary>
public interface CarFactory
{
CarEngine CreateEngine();
CarAircondition CreateAircondition();
}
public class CarBMW200 : CarFactory
{
public CarEngine CreateEngine()
{
return new CarEngine1();
}
public CarAircondition CreateAircondition()
{
return new CarAircondition1();
}
}
public class CarBMW300 : CarFactory
{
public CarEngine CreateEngine()
{
return new CarEngine2();
}
public CarAircondition CreateAircondition()
{
return new CarAircondition2();
}
}
2、工厂模式实例
使用Unity时,会很经常的遇到需要加载资源的问题。这时候就特别适合使用工厂模式,在工厂进行资源加载,我们只需要获取工厂加载好的资源就可以了
工厂模式例子代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 工厂模式
/// </summary>
public class FactoryMode : MonoBehaviour
{
private void Start()
{
//获取加载的资源
GameObject character = FactoryManager.ResourcesAssetFactory.LoadCharacter("Soldier");
AudioClip audioClip = FactoryManager.ResourcesAssetFactory.LoadAudioClip("yinyue");
Sprite sprite = FactoryManager.ResourcesAssetFactory.LoadSprite ("tupian");
}
}
/// <summary>
/// 资源工厂---加载资源
/// </summary>
public interface IAssetFactory
{
/// <summary>
/// 加载角色模型
/// </summary>
GameObject LoadCharacter(string name);
/// <summary>
/// 加载声音Clip资源
/// </summary>
AudioClip LoadAudioClip(string name);
/// <summary>
/// 加载精灵Sprite图片
/// </summary>
Sprite LoadSprite(string name);
}
/// <summary>
/// 从Unity的Resources文件夹中加载
/// </summary>
public class ResourcesLoadAssetFactory : IAssetFactory
{
//记录资源在Resources文件夹中的路径
private const string CharacterPath = "Character/";
private const string AudioClipPath = "AudioClip/";
private const string SpritePath = "Sprite/";
public GameObject LoadCharacter(string name)
{
return LoadAsset(CharacterPath + name);
}
public AudioClip LoadAudioClip(string name)
{
AudioClip audioClip = Resources.Load(AudioClipPath + name) as AudioClip;
if (audioClip == null)
{
Debug.LogError("无法加载路径:" + AudioClipPath + name);
return null;
}
return audioClip;
}
public Sprite LoadSprite(string name)
{
Sprite sprite = Resources.Load(SpritePath + name) as Sprite;
if (sprite == null)
{
Debug.LogError("无法加载路径:" + SpritePath + name);
return null;
}
return sprite;
}
private GameObject LoadAsset(string path)
{
GameObject go = Resources.Load(path) as GameObject;
if (go == null)
{
Debug.LogError("无法加载路径:" + path);
return null;
}
return go;
}
}
/// <summary>
/// 从服务器加载---暂时还不会
/// </summary>
public class RemoteAssetFactory : IAssetFactory
{
public AudioClip LoadAudioClip(string name)
{
throw new System.NotImplementedException();
}
public GameObject LoadCharacter(string name)
{
throw new System.NotImplementedException();
}
public Sprite LoadSprite(string name)
{
throw new System.NotImplementedException();
}
}
/// <summary>
/// 工厂管理静态类,方便获取加载的资源
/// </summary>
public static class FactoryManager
{
private static IAssetFactory mResourcesAssetFactory = null;
public static IAssetFactory ResourcesAssetFactory
{
get
{
if (mResourcesAssetFactory == null)
{
mResourcesAssetFactory = new ResourcesLoadAssetFactory();
}
return mResourcesAssetFactory;
}
}
}