sportdog

导航

 
 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(应用场景)

posted on 2014-07-16 16:25  sportdog  阅读(144)  评论(0编辑  收藏  举报