赞
踩
public class SingLeton
{
private static SingLeton instance;
private SingLeton()
{
instance = new SingLeton();
}
public static SingLeton Instace
{
get{return instance;}
}
}
2.懒汉模式:第一次调用时才创建该单例对象
public class LazySingleton
{
private static LSingleton _instance;
public static Singleton Instace
{
get
{
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}
}
public sealed class Singleton
{
private Singleton() { }
private static Singleton _instance;
public static Singleton GetInstance()
{
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
public void someBusinessLogic()
{
// ...
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。