第1章 单例模式(Single Pattern)

原文 第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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值