首页 > 其他 > 详细

设计模式之单例模式

时间:2014-07-22 22:54:06      阅读:253      评论:0      收藏:0      [点我收藏+]
 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(应用场景)

设计模式之单例模式,布布扣,bubuko.com

设计模式之单例模式

原文:http://www.cnblogs.com/sportdog/p/3848971.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!