码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之单例模式

时间:2014-07-22 22:55:15      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   art   re   

 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

设计模式之单例模式

标签:style   blog   http   color   art   re   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!