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

第1章 单例模式(Single Pattern)

时间:2014-05-12 08:37:16      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:class   code   tar   c   http   int   

原文 第1章 单例模式(Single Pattern)

 单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点。

 一、常用模式:

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

这种模式只适用在单线程下,并没有考虑到线程并发的问题,在多线程中,可能出现两个线程同时获取Instance的实例,可能会出现多个实例对象。

二、多线程下Lazy模式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Singleton
{
       private static Singleton instance;
       private static object _lock=new object();
  
       private Singleton()
       {
  
       }
  
       public static Singleton GetInstance()
       {
               if(instance==null)
               {
                      lock(_lock)
                      {
                             if(instance==null)
                             {
                                     instance=new Singleton();
                             }
                      }
               }
               return instance;
       }
}

使用了Lock锁,可以较好的解决多线程下单例模式的实现,lock锁保证同一时间内只有一个线程能访问Lock里面的语句块,比较有效的解决性能的开销。

三、饿汉模式

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

该类标记为 sealed 以阻止发生派生,而派生可能会增加实例。使用的readonly关键可以跟static一起使用,用于指定该常量是类别级的,它的初始化交由静态构造函数实现,并可以在运行时编译。在这种模式下,无需自己解决线程安全性问题,CLR会给我们解决。由此可以看到这个类被加载时,会自动实例化这个类,而不用在第一次调用GetInstance()后才实例化出唯一的单例对象。

四、延迟加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public sealed class Singleton
 {
    Singleton()
     {
     }
   
     public static Singleton Instance
     {
         get
        {
            return Nested.instance;
        }
    }
      
    class Nested
    {
        static Nested()
        {
        }
  
        internal static readonly Singleton instance = new Singleton();
    }
}

这种方法用的比较的少,但是也是一种不错的方法。

 

   设计模式系列文章入口http://www.diyibk.com/post/39.html

第1章 单例模式(Single Pattern),布布扣,bubuko.com

第1章 单例模式(Single Pattern)

标签:class   code   tar   c   http   int   

原文地址:http://www.cnblogs.com/lonelyxmas/p/3720808.html

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