设计模式:单件模式

简介:

Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点。

1. 单线程时方法

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

这句if (instance == null)不是线程安全的,可能产生多个实例。

2.线程安全的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public  sealed  class  Singlton
{
     static  Singlton instance = null ;
 
     static  readonly  object  o = new  object ();
 
     Singlton()
     { }
 
     public  static  Singlton Instance
     {
         get
         {
             lock  (o)
             {
                 if  (instance == null )
                 {
                     return  new  Singlton();
                 }
                 return  instance;
             }
         }   
     }   
}

对象实例由最先进入的那个线程创建,后来的线程在进入时(instence == null)为假,不会再去创建对象实例了。但是这种实现方式增加了额外的开销,损失了性能。

3. 双重锁定

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
26
27
public  sealed  class  Singlton
{
     static  Singlton instance = null ;
 
     static  readonly  object  o = new  object ();
 
     Singlton()
     { }
 
     public  static  Singlton Instance
     {
         get
         {
             if  (instance == null )
             {
                 lock  (o)
                 {
                     if  (instance == null )
                     {
                         return  new  Singlton();
                     }
                 }
             }
             return  instance;
         }
     }
}

避免了每个 Instance 属性方法的调用中都出现独占锁定。

4. 静态初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public  sealed  class  Singlton
{
     static  readonly  Singlton instance = new  Singlton();
 
     static  Singlton()
     { }
 
     public  static  Singlton Instance
     {
         get
         {
             return  instance;
         }
     }
}

5. 延迟静态初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public  sealed  class  Singlton
{      
 
     static  Singlton()
     { }
     
     public  static  Singlton Instance
     {
         get
         {
             return  CreateSinglton.instance;
         }
     }
     
     class  CreateSinglton
     {
         internal  static  readonly  Singlton instance = new  Singlton();
     
         static  CreateSinglton() { }
     }
}
本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2009/11/26/1611183.html如需转载请自行联系原作者

王德水
相关文章
|
PHP 数据库
设计模式(二)单件模式Singleton(创建型)
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> SINGLETON(单件)—对象创建型模式</p> <p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"
1065 0
|
程序员 PHP 设计模式
php设计模式总结-单件模式
一、单件模式英文叫做sington。其他语言中有叫做单例模式,其实都是一样的道理。保证只会出现单个实例,所以是单例。翻译成单件,永远只会产生一件,呵呵。 还有翻译成单元素模式。其实关键是看这个英文比较好。
889 0
.NET简谈设计模式之(单件模式)
我们继续学习设计模式系列文章。 今天要讲的是单件模式,其实单件模式是比较简单的设计模式,在我们日常开发过程中也是经常用到的。 单件模式:单件模式是一种用于确保整个应用程序中只有一个类实例且这个实例所占资源在整个应用程序中是共享时的程序设计方法。
650 0
|
安全 C# 编译器
温故而知新:设计模式之单件模式(Singleton)
 1 using System; 2  3 namespace Singleton 4 { 5     class Program 6     { 7         static void Main(string[] args) 8         { 9             Singleton s1 = Singleton.
868 0
|
PHP 设计模式
设计模式-PHP实现单件模式的几种方式
单件模式是我们在开发中经常用到的一种设计模式,利用PHP5面向对象的特性,我们可以很容易的构建单件模式的应用,下面是单件模式在PHP中的几种实现方法: class Stat{     static $instance = NULL;          static function get...
870 0