单件模式笔记
单件模式要求一个类有且仅有一个实例,同时提供了一个全局访问点.
单线程Singleton模式实现
public class Singleton
{
private static Singleton singleton;
private Singleton(){ }
public static Singleton Singleton
{ get
{ if(singleton==null) {singleton=new Singleton();} return singleton;
}
}
多线程Singleton模式的实现
Œ
public class Singleton
{
public static readonly Singleton Singleton=new Singleton();
private Singleton(){}
}
public class Singleton
{
public static readonly Singleton Singleton;
static Singleton{Singlenton=new Singlenton();}
private Singleton(){}
}
单线程Singleton模式实现
public class Singleton
{
private static Singleton singleton;
private Singleton(){ }
public static Singleton Singleton
{ get
{ if(singleton==null) {singleton=new Singleton();} return singleton;
}
}
多线程Singleton模式的实现
Œ
public class Singleton
{
public static readonly Singleton Singleton=new Singleton();
private Singleton(){}
}
public class Singleton
{
public static readonly Singleton Singleton;
static Singleton{Singlenton=new Singlenton();}
private Singleton(){}
}