摘錄自:設計模式與游戲完美開發
十年磨一劍,作者將設計模式理論巧妙地融入到實踐中,以一個游戲的完整實現呈現設計模式的應用及經驗的傳承 《軒轅劍》之父——蔡明宏、資深游戲制作人——李佳澤、Product Evangelist at Unity Technologies——Kelvin Lo、信仁軟件設計創辦人—— 賴信仁、資深3D游戲美術——劉明愷 聯合推薦全書采用了整合式的項目教學,即以一個游戲的范例來應用23種設計模式的實現貫穿全書,讓讀者學習到整個游戲開發的全過程和作者想要傳承的經驗,并以淺顯易懂的比喻來解析難以理解的設計模式,讓想深入了解此領域的讀者更加容易上手。
工程GitHub
FLYWEIGHT—每天跟MM發短信,手指都累死了,最近買了個新手機,可以把一些常用的句子存在手機里,要用的時候,直接拿出來,在前面加上MM的名字就可以發送了,再不用一個字一個字敲了。共享的句子就是Flyweight,MM的名字就是提取出來的外部特征,根據上下文情況使用。
享元模式:FLYWEIGHT在拳擊比賽中指最輕量級。享元模式以共享的方式高效的支持大量的細粒度對象。享元模式能做到共享的關鍵是區分內蘊狀態和外蘊狀態。內蘊狀態存儲在享元內部,不會隨環境的改變而有所不同。外蘊狀態是隨環境的改變而改變的。外蘊狀態不能影響內蘊狀態,它們是相互獨立的。將可以共享的狀態和不可以共享的狀態從常規類中區分開來,將不可以共享的狀態從類里剔除出去。客戶端不可以直接創建被共享的對象,而應當使用一個工廠對象負責創建被共享的對象。享元模式大幅度的降低內存中對象的數量。
using UnityEngine;
using System.Collections.Generic;
namespace DesignPattern_Flyweight
{
// 可以被共用的Flyweight介面
public abstract class Flyweight
{
protected string m_Content; //顯示的內容
public Flyweight(){}
public Flyweight(string Content)
{
m_Content= Content;
}
public string GetContent()
{
return m_Content;
}
public abstract void Operator();
}
// 共用的元件
public class ConcreteFlyweight : Flyweight
{
public ConcreteFlyweight(string Content):base( Content )
{
}
public override void Operator()
{
Debug.Log("ConcreteFlyweight.Content["+m_Content+"]");
}
}
// 不共用的元件(可以不必繼承)
public class UnsharedConcreteFlyweight //: Flyweight
{
Flyweight m_Flyweight = null; // 共享的元件
string m_UnsharedContent; // 不共享的元件
public UnsharedConcreteFlyweight(string Content)
{
m_UnsharedContent = Content;
}
// 設定共享的元件
public void SetFlyweight(Flyweight theFlyweight)
{
m_Flyweight = theFlyweight;
}
public void Operator()
{
string Msg = string.Format("UnsharedCoincreteFlyweight.Content[{0}]",m_UnsharedContent);
if( m_Flyweight != null)
Msg += "包含了:" + m_Flyweight.GetContent();
Debug.Log(Msg);
}
}
// 負責產生Flyweight的工廠介面
public class FlyweightFactor
{
Dictionary<string,Flyweight> m_Flyweights = new Dictionary<string,Flyweight>();
// 取得共用的元件
public Flyweight GetFlyweight(string Key,string Content)
{
if( m_Flyweights.ContainsKey( Key) )
return m_Flyweights[Key];
// 產生并且設定內容
ConcreteFlyweight theFlyweight = new ConcreteFlyweight( Content );
m_Flyweights[Key] = theFlyweight;
Debug.Log ("New ConcreteFlyweigh Key["+Key+"] Content["+Content+"]");
return theFlyweight;
}
// 取得元件(只取得不共用的Flyweight)
public UnsharedConcreteFlyweight GetUnsharedFlyweight(string Content)
{
return new UnsharedConcreteFlyweight( Content);
}
// 取得元件(包含共用部份的Flyweight)
public UnsharedConcreteFlyweight GetUnsharedFlyweight(string Key,string SharedContent,string UnsharedContent)
{
// 先取得共用的部份
Flyweight SharedFlyweight = GetFlyweight(Key, SharedContent);
// 產出元件
UnsharedConcreteFlyweight theFlyweight = new UnsharedConcreteFlyweight( UnsharedContent);
theFlyweight.SetFlyweight( SharedFlyweight ); // 設定共享的部份
return theFlyweight;
}
}
}
using UnityEngine;
using System.Collections;
using DesignPattern_Flyweight;
public class FlyweightTest : MonoBehaviour {
// Use this for initialization
void Start () {
UnitTest();
}
void UnitTest () {
// 元件工廠
FlyweightFactor theFactory = new FlyweightFactor();
// 產生共用元件
theFactory.GetFlyweight("1","共用元件1");
theFactory.GetFlyweight("2","共用元件1");
theFactory.GetFlyweight("3","共用元件1");
// 取得一個共用元件
Flyweight theFlyweight = theFactory.GetFlyweight("1","");
theFlyweight.Operator();
// 產生不共用的元件
UnsharedConcreteFlyweight theUnshared1 = theFactory.GetUnsharedFlyweight("不共用的資訊1");
theUnshared1.Operator();
// 設定共用元件
theUnshared1.SetFlyweight( theFlyweight );
// 產生不共用的元件2,並指定使用共用元件1
UnsharedConcreteFlyweight theUnshared2 = theFactory.GetUnsharedFlyweight("1","","不共用的資訊2");
// 同時顯示
theUnshared1.Operator();
theUnshared2.Operator();
}
}