DesignPattern_AbstractFactory
摘錄自:設(shè)計(jì)模式與游戲完美開(kāi)發(fā)
工程GitHub
SINGLETON—俺有6個(gè)漂亮的老婆,她們的老公都是我,我就是我們家里的老公Singleton,她們只要說(shuō)道“老公”,都是指的同一個(gè)人,那就是我(剛才做了個(gè)夢(mèng)啦,哪有這么好的事)
單例模式:?jiǎn)卫J酱_保某一個(gè)類(lèi)只有一個(gè)實(shí)例,而且自行實(shí)例化并向整個(gè)系統(tǒng)提供這個(gè)實(shí)例單例模式。單例模式只應(yīng)在有真正的“單一實(shí)例”的需求時(shí)才可使用。
using UnityEngine;
using System.Collections;
namespace DesignPattern_Singleton
{
// 單例模式
public class Singleton
{
public string Name {get; set;}
private static Singleton _instance;
public static Singleton Instance
{
get
{
if (_instance == null)
{
Debug.Log("產(chǎn)生Singleton");
_instance = new Singleton();
}
return _instance;
}
}
private Singleton(){}
}
}
using UnityEngine;
using System.Collections;
using DesignPattern_Singleton;
public class SingletonTest : MonoBehaviour {
// Use this for initialization
void Start () {
UnitTest();
UnitTest_ClassWithCounter();
}
// 單例模式測(cè)試方法
void UnitTest ()
{
Singleton.Instance.Name = "Hello";
Singleton.Instance.Name = "World";
Debug.Log (Singleton.Instance.Name);
//Singleton TempSingleton = new Singleton();// 錯(cuò)誤 error CS0122: `DesignPattern_Singleton.Singleton.Singleton()' is inaccessible due to its protection level
}
// 有計(jì)數(shù)功能類(lèi)別的測(cè)試方法
void UnitTest_ClassWithCounter ()
{
// 有計(jì)數(shù)功能的類(lèi)別
ClassWithCounter pObj1 = new ClassWithCounter();
pObj1.Operator();
ClassWithCounter pObj2 = new ClassWithCounter();
pObj2.Operator();
pObj1.Operator();
}
}
-
一種是具有計(jì)數(shù)功能的類(lèi),來(lái)達(dá)到單例的效果
using UnityEngine;
using System.Collections;
// 有計(jì)數(shù)功能的類(lèi)別
public class ClassWithCounter
{
protected static int m_ObjCounter = 0;
protected bool m_bEnable=false;
public ClassWithCounter()
{
m_ObjCounter++;
m_bEnable = ( m_ObjCounter ==1 )? true:false ;
if( m_bEnable==false)
Debug.LogError("目前物件數(shù)["+m_ObjCounter+"]超過(guò)1個(gè)!!");
}
public void Operator()
{
if( m_bEnable ==false)
return ;
Debug.Log ("可以執(zhí)行");
}
}
-
還有一種比較安全的單例寫(xiě)法
public class Singleton
{
public const string Name = "Singleton";
static Singleton() { }
protected Singleton() { }
protected static volatile Singleton m_instance = null;
protected readonly object m_syncRoot = new object();
protected static readonly object m_staticSyncRoot = new object();
public static Singleton Instance
{
get
{
if (m_instance == null)
{
lock (m_staticSyncRoot)
{
if (m_instance == null) m_instance = new Singleton();
}
}
return m_instance;
}
//set { m_instance = value; }
}
}
Unity版
public class Singleton
{
public const string Name = "Singleton";
static Singleton() { }
protected Singleton() { }
protected static volatile Singleton m_instance = null;
protected readonly object m_syncRoot = new object();
protected static readonly object m_staticSyncRoot = new object();
public static Singleton Instance
{
get
{
if (m_instance == null)
{
lock (m_staticSyncRoot)
{
if (m_instance == null)
{
GameObject Obj = new GameObject("Singleton ", typeof(Singleton ));
instance = Obj.GetComponent<Singleton >();
}
}
}
return m_instance;
}
//set { m_instance = value; }
}
}