创建型模式-单件模式(1)

单件模式特点:

       1 :单件类只可有一个实例。

       2 :单件类必须自己创建自己这惟一的实例。

       3 :单件类必须给所有其他对象提供这一实例。

结构:

image

如何实现:

其实我们只需要把构造函数私有化,把new对象的过程进行控制就行了,具体代码如下:

 1public class Singleton
 2    {
 3        private static Singleton uniqueInstance; // 用一个静态变量来记录Singleton类的唯一实例
 4
 5        private Singleton()   // 构造器必须私有
 6        {
 7        }

 8
 9        public static Singleton Instance
10        {
11            get
12            {
13                if (null == uniqueInstance) // 如果uniqueInstance为空,说明对象没有被创建
14                {
15                    uniqueInstance = new Singleton();
16                }

17                return uniqueInstance;
18            }

19        }

20    }

21
但是上面的代码存在一个问题。当使用多线程时,产生的对象可能不只一个,分析见下图:
image 
如何解决这个问题:
a)、使用加锁机制,具体代码如下:
 1public class Singleton
 2    {
 3        private static Singleton uniqueInstance; // 用一个静态变量来记录Singleton类的唯一实例
 4        private static Object obj = new object();
 5        
 6        private Singleton()   // 构造器必须私有
 7        {
 8        }

 9
10        public static Singleton Instance
11        {
12            get
13            {
14                lock (obj)                      // 使用加锁,避免两个线程同时进入
15                {
16                    if (null == uniqueInstance) // 如果uniqueInstance为空,说明对象没有被创建
17                    {
18                        uniqueInstance = new Singleton();
19                    }

20                    return uniqueInstance;
21                }

22            }

23        }

24    }

25

b)、使用“急切”创建实例,不用延迟加载。具体代码如下:

 1public class Singleton
 2    {
 3        static Singleton uniqueInstance = new Singleton();  // 用一个静态变量来记录Singleton类的唯一实例
 4
 5        private Singleton()   // 构造器必须私有
 6        {
 7        }

 8
 9        public static Singleton Instance
10        {
11            get
12            {
13                return uniqueInstance;
14            }

15        }

16    }

17

c)、使用“双检锁”机制。具体代码如下:

 1public class Singleton
 2    {
 3        static Singleton uniqueInstance = null;  // 用一个静态变量来记录Singleton类的唯一实例
 4        private static Object obj = new object();
 5
 6        private Singleton()   // 构造器必须私有
 7        {
 8        }

 9
10        public static Singleton Instance
11        {
12            get
13            {
14                if (null == uniqueInstance)         // 先判断uniqueInstance是否为空,如果为空,再进行加锁 
15                {
16                    lock (obj)                      // 使用加锁,避免两个线程同时进入
17                    {
18                        if (null == uniqueInstance) // 如果uniqueInstance为空,说明对象没有被创建
19                        {
20                            uniqueInstance = new Singleton();
21                        }
                        
22                    }

23                }

24                return uniqueInstance;
25            }

26        }

27    }

28

对上面三种方法的评价:

1)、a方法采用了加锁机制,每次实例化都必须加锁,而加锁耗费的系统资源比较多,因此执行效率比较低,不推荐使用。

2)、b方法书写比较简单,但没有采用延迟加载,所以可能浪费部分资源。当使用频率高且运行时负担不重时推荐使用。

3)、c方法解决了多线程问题,并且只在第一次创建对象时加锁,执行效率比a方法高,推荐使用。

在什么情形下使用单例模式:

使用Singleton模式有一个必要条件:在一个系统要求一个类只有一个实例时才应当使用单例模式。反过来,如果一个类可以有几个实例共存,就不要使用单例模式。

注意:

a)、不要使用单例模式存取全局变量。这违背了单例模式的用意,最好放到对应类的静态成员中。

b)、不要将数据库连接做成单例,因为一个系统可能会与数据库有多个连接,并且在有连接池的情况下,应当尽可能及时释放连接。Singleton模式由于使用静态成员存储类实例,所以可能会造成资源无法及时释放,带来问题。


 转自:《Head.First设计模式》的学习笔记(6)--单件模式

posted @ 2010-06-08 18:51  clound  阅读(328)  评论(0编辑  收藏  举报