单件模式singleton的理解
时间:2010-08-03 来源:fucuihong
public class LogWriter { //申明一个静态的变量,类型为类本身 private static LogWriter _instance = null;
//将类的构造函数私有化,使得这个类不可以被外界创建 private LogWriter() { } //提供静态的方法,创建类的实例:单件模式 public static LogWriter GetInstance() { if (_instance == null) { _instance = new LogWriter(); } return _instance; } } |