首页 > 代码库 > 设计模式之单例模式

设计模式之单例模式

 private static Singleton instance;        public Singleton()        {             //private 不让客户端 new        }        public static Singleton GetInstance()        {            if (instance == null)            {                instance = new Singleton();            }            return instance;        }


客户端

 protected void Page_Load(object sender, EventArgs e)        {            Singleton s1 = Singleton.GetInstance();            Singleton s2 = Singleton.GetInstance();            if (s1 == s2)            {                Response.Write("我们是同一个<br>");             }            Singleton s3 = new Singleton();            Singleton s4 = new Singleton();            if (s3 != s4)            {                Response.Write("我们不是同一个实例");             }        }

比较简单,作个笔记.

http://blog.csdn.net/likika2012/article/details/11483167(应用场景)