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

浅析设计模式(九)——创建型模式之Singleton(单例模式)

时间:2018-08-27 23:21:44      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:public   let   ret   thread   locking   creation   variable   name   style   

单例模式

本文的内容:

一、单例模式的定义

  定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点。

       单例,顾名思义,就是只有一个实例,因此需要将构造函数私有化,使其不能随意实例化。

  好处:减少对象的创建和销毁,提高性能;资源共享;

二、单例模式的示例

1、经典的单例模式:非线程安全的写法。

 1 public class ClassicSingleton {
 2     private static ClassicSingleton uniqueInstance;
 3      
 4     private ClassicSingleton() {}
 5  
 6     //not thread safe
 7     public static ClassicSingleton getInstance() {
 8         if (null == uniqueInstance) {
 9             uniqueInstance = new ClassicSingleton();
10         }
11         return uniqueInstance;
12     }
13  
14     // other useful methods here
15     public String getDescription() {
16         return "I‘m a classic ClassicSingleton!";
17     }
18 }

 

2、线程安全的做法

  对上面的经典形式进行同步,使其在多线程环境下变得安全:

 1 public class ThreadSafeSingleton {
 2     private static ThreadSafeSingleton uniqueInstance;
 3      
 4     // other useful instance variables here
 5  
 6     private ThreadSafeSingleton() {}
 7  
 8     public static synchronized ThreadSafeSingleton getInstance() {
 9         if (null == uniqueInstance) {
10             uniqueInstance = new ThreadSafeSingleton();
11         }
12         return uniqueInstance;
13     }
14  
15     // other useful methods here
16     public String getDescription() {
17         return "I‘m a thread safe ThreadSafeSingleton!";
18     }
19 }

 

3、双重检查加锁

  注意到这里内部实例加了volatile修饰,保证线程的可见性。

 1 public class DoubleCheckLockingSingleton {
 2     private volatile static DoubleCheckLockingSingleton uniqueInstance;
 3      
 4     private DoubleCheckLockingSingleton() {}
 5  
 6     public static DoubleCheckLockingSingleton getInstance() {
 7         if (null == uniqueInstance) {
 8             synchronized (DoubleCheckLockingSingleton.class) {
 9                 if (null == uniqueInstance) {
10                     uniqueInstance = new DoubleCheckLockingSingleton();
11                 }
12             }
13         }
14         return uniqueInstance;
15     }
16 }

 

4、内部类实现单例

 1 public class InnerClassSingleton {
 2     private InnerClassSingleton() {}
 3 
 4     public static InnerClassSingleton getInstance() {
 5         return Nested.instance;
 6     }
 7 
 8     private static class Nested {
 9         public static InnerClassSingleton instance = new InnerClassSingleton();
10     }
11 }

 

5、恶汉模式单例:借助类的惟一一次初始化进行加载

 1 public class HungrySingleton {
 2     
 3     private static HungrySingleton uniqueInstance = new HungrySingleton();
 4      
 5     private HungrySingleton() {}
 6  
 7     public static HungrySingleton getInstance() {
 8         return uniqueInstance;
 9     }
10     
11     // other useful methods here
12     public String getDescription() {
13         return "I‘m a statically initialized Singleton!";
14     }
15 }

 

三、参考

1、参考《Head First设计模式》和GoF《设计模式:可复用面向对象软件的基础》

2、代码可参考【github传送门】。

待办:单例如何回收、内部安全问题等。

 

浅析设计模式(九)——创建型模式之Singleton(单例模式)

标签:public   let   ret   thread   locking   creation   variable   name   style   

原文地址:https://www.cnblogs.com/wpbxin/p/8871644.html

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