濟(jì)南專(zhuān)門(mén)做公司網(wǎng)站的公司漯河網(wǎng)站seo
目錄
抽象工廠模式
思維導(dǎo)圖
接口(抽象類(lèi))
工廠接口
抽象產(chǎn)品類(lèi)
抽象武器接口
抽象人物接口
具體工廠和具體產(chǎn)品
具體工廠
(1)產(chǎn)品接口,生成具體人物
(2)武器接口,生成具體武器
具體產(chǎn)品的實(shí)現(xiàn)
Soldier類(lèi)型
ShotGunA 類(lèi)型
單例模式資源加載
測(cè)試
GameController
PerformanceTest
抽象工廠模式
思維導(dǎo)圖
一個(gè)工廠里面可以生產(chǎn)多個(gè)產(chǎn)品
一個(gè)工廠可以生產(chǎn)一系列產(chǎn)品(一族產(chǎn)品),極大減少了工廠類(lèi)的數(shù)量
接口(抽象類(lèi))
工廠接口
在工廠里聲明創(chuàng)造的武器和產(chǎn)品
武器:創(chuàng)造武器方法創(chuàng)造具體的武器
人物:創(chuàng)造產(chǎn)品方法創(chuàng)造具體的人物
public interface IGameFactory
{IWeapon GreateWeapon();ICharacter CreateCharacter();
}
抽象產(chǎn)品類(lèi)
抽象武器接口
用來(lái)生產(chǎn)不同的武器,武器類(lèi)型
/// <summary>
/// 抽象武器接口-抽象產(chǎn)品
/// </summary>
public interface IWeapon
{void Use();//使用武器void Display();//顯示武器
}
抽象人物接口
用來(lái)生成不同的產(chǎn)品,人物類(lèi)型
/// <summary>
/// 抽象產(chǎn)品
/// </summary>
public interface ICharacter
{void Display();//顯示模型
}
具體工廠和具體產(chǎn)品
現(xiàn)代風(fēng)格的具體工廠,返回具體的產(chǎn)品
具體工廠
(1)產(chǎn)品接口,生成具體人物
返回要生成的產(chǎn)品,Soldier類(lèi)型
(2)武器接口,生成具體武器
public class ModernGameFactory : IGameFactory
{public ICharacter CreateCharacter(){return new Soldier();}public IWeapon GreateWeapon(){return new ShotGunA();}
}
具體產(chǎn)品的實(shí)現(xiàn)
Soldier類(lèi)型
實(shí)現(xiàn)ICharacter接口,生產(chǎn)具體的人物
/// <summary>
/// 具體產(chǎn)品---士兵
/// </summary>
public class Soldier : ICharacter
{private GameObject _model;public Soldier(){_model = ResourceManager.Instance.GetResource("Bot/SoldierA");}public void Display(){if (_model != null){GameObject.Instantiate(_model);}else{Debug.LogError("Soldier model not found");}}
}
ShotGunA 類(lèi)型
ShotGunA產(chǎn)品的生產(chǎn)
實(shí)現(xiàn)IWeapon接口
public class ShotGunA : IWeapon
{private GameObject _model;public ShotGunA(){_model = ResourceManager.Instance.GetResource("Weapon/LaserGun_A");}public void Display(){if (_model != null){GameObject.Instantiate(_model);}else{Debug.LogError("ShotGunA model not found");}}public void Use(){Debug.Log("使用武器");}
}
單例模式資源加載
單例模式(Singleton Pattern):是一種創(chuàng)建對(duì)象的設(shè)計(jì)模式,確保一個(gè)類(lèi)只有一個(gè)實(shí)例,并提供全局訪問(wèn)點(diǎn)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// 單例模式
/// </summary>
public class ResourceManager
{//本類(lèi)實(shí)例的引用private static ResourceManager _instance;//資源緩存器private Dictionary<string, GameObject> _resourceCache = new Dictionary<string, GameObject>();//為私有的字段準(zhǔn)備的屬性public static ResourceManager Instance{get//保證有且只有一個(gè)實(shí)例{if (_instance == null){_instance = new ResourceManager();}return _instance;}}//獲取資源的工作代碼,從硬盤(pán)或者緩存中獲取模型資源//傳入路徑pathpublic GameObject GetResource(string path){//詢(xún)問(wèn)資源存儲(chǔ)器中是否包含當(dāng)前路徑(不需要重復(fù)加載)if (!_resourceCache.ContainsKey(path)){GameObject resource = Resources.Load<GameObject>(path);if (resource == null){Debug.LogError($"Failed to load resource at path: {path}");return null;}_resourceCache[path] = resource;}return _resourceCache[path];}
}
測(cè)試
GameController
建個(gè)空物體掛上即可
功能:創(chuàng)建產(chǎn)品進(jìn)行測(cè)試
public class GameController : MonoBehaviour
{private ICharacter _character;private IWeapon _weapon;public void StartGame(IGameFactory factory){try{_character = factory.CreateCharacter(); _weapon = factory.GreateWeapon();_character.Display();_weapon.Use();}catch (System.Exception e){Debug.LogError($"Error starting game: {e.Message}");}}
}
PerformanceTest
建個(gè)空物體掛上即可
功能:測(cè)試用抽象工廠模式創(chuàng)建物體和直接實(shí)例化物體的時(shí)間性能區(qū)別
(直接創(chuàng)建會(huì)快)
public class PerformanceTest : MonoBehaviour
{private void Start(){//TestDirectInstantiation(500);TestFactoryPattern(500);}//直接實(shí)例化void TestDirectInstantiation(int count){Stopwatch stopwatch = new Stopwatch();stopwatch.Start();List<GameObject> objects = new List<GameObject>();GameObject prefab = Resources.Load<GameObject>("Bot/SoldierA");for (int i = 0; i < count; i++){objects.Add(GameObject.Instantiate(prefab));}stopwatch.Stop();UnityEngine.Debug.Log($"Direct Instantiation ({count} objects): {stopwatch.ElapsedMilliseconds} ms");// Clean upforeach (var obj in objects){GameObject.Destroy(obj);}objects.Clear();Resources.UnloadUnusedAssets();}//抽象工廠的實(shí)例化測(cè)試void TestFactoryPattern(int count){Stopwatch stopwatch = new Stopwatch();stopwatch.Start();IGameFactory factory = new ModernGameFactory();List<ICharacter> characters = new List<ICharacter>();for (int i = 0; i < count; i++){characters.Add(factory.CreateCharacter());characters[i].Display();}stopwatch.Stop();UnityEngine.Debug.Log($"Factory Pattern ({count} objects): {stopwatch.ElapsedMilliseconds} ms");//Clean upcharacters.Clear();Resources.UnloadUnusedAssets();}}