c#简单的实现单例模式,或单体模式
s
abstract public class DefaultSingletonNode<TNode> where TNode : DefaultSingletonNode<TNode>, new() { private static object lockHelper = new object(); public static TNode GetInstance() { TNode _instance = null; if (_instance == null) { lock (lockHelper) { if (_instance == null) { _instance = new TNode(); } } } return _instance; } }
abstract public class SingleBLL<T> : DefaultNode<T> where T : DefaultNode<T>, new(){ }
public class logBLL : SingleBLL<logBLL> {
public void Write(string s){} }
调用
logBLL.GetInstance().Write("sdfsfdsf");
优点,对于类型的实例化具有一定的可控性,不需要一直去new,