這是我的理解,我可以實現在C#中單件模式提供接入負全責:我怎樣才能讓一個類,用於創建和另一個類
public class ChesneyHawkes{
private static ChesneyHawkes _instance = new ChesneyHawkes();
public ChesneyHawkes Instance {get{return _instance;}}
private ChesneyHawkes()
{
}
}
如果我想提供的單個實例一個對象,以至於只能有一個,公開訪問它的對象,但只允許它被創建或被另一個單例替代。
// The PuppetMaster should be the only class that
// can create the only existing Puppet instance.
public class PuppetMaster{
private static PuppetMaster_instance = new PuppetMaster();
public static PuppetMaster Instance {get{return _instance;}}
// Like a singleton but can be replaced at the whim of PuppetMaster.Instance
public static Puppet PuppetInstance {get {return Puppet;}}
private PuppetMaster()
{
}
public class Puppet{
// Please excuse the pseudo-access-modifier
puppetmasteronly Puppet(){
}
}
}
// To be accessed like so.
PuppetMaster.Puppet puppet = PuppetMaster.Instance.PuppetInstance;
您是否在嘗試在詢問之前搜索此站點以獲取「Singleton」? – 2010-02-23 15:37:59