首页 > 其他 > 详细

单例模式

时间:2016-06-11 14:21:51      阅读:162      评论:0      收藏:0      [点我收藏+]

普通模式:判断是否为空,如果没有instance,就new 一个;

 1 public class Singleton
 2 {
 3     private static Singleton instance;
 4     private Singleton()
 5     {
 6     }
 7     public Singleton GetInstance()
 8     {
 9         if(instance == null)
10         {
11             instance = new Singleton();
12         }
13         return instance;
14     }            
15 }

 

懒汉模式:直接在编译时new一个实例;也可以解决多线程的问题;

 1 public class Singleton
 2 {
 3     private static readonly Singleton instance = new Singleton();
 4     private Singleton()
 5     {
 6     }
 7     public Singleton GetInstance()
 8     {
 9         return instance;
10     }            
11 }

 

单例模式

原文:http://www.cnblogs.com/zengneng/p/5575228.html

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