单例模式(Singleton pattern)四种实现

  • 单例实现1:经典单例模式(Classic singleton pattern)

  1. 实现延迟实例化(Lazy instantiaze);线程不安全(thread-unsafe)
  2. 可用来学习单例模式思想,但是因为线程不安全,所以不建议使用

Java代码   收藏代码
  1. /** 
  2.  * Classic singleton pattern      
  3.  * @author <a href="mailto:ifuteng@gmail.com">futeng</a> 
  4.  */  
  5. public class Singleton {  
  6.       // private static variable  
  7.       private static Singleton singleton;  
  8.         
  9.       // private constructor  
  10.       private Singleton() {  
  11.              // do something useful such as initialized data  
  12.       }  
  13.         
  14.       // provides a global point of access to it  
  15.       public static Singleton getInstance() {  
  16.              if ( singleton == null) {  
  17.                    singleton = new Singleton();  
  18.             }  
  19.              return singleton;  
  20.       }  
  21. }  


  • 单例实现2:对方法同步加锁式单例(Synchronized method singleton pattern)

  1. 实现延迟实例化(Lazy instantiaze);线程安全(thread safe);每次访问都要等候别的线程离开该方法,性能低。
  2. 解决了经典单例模式线程不安全的问题,但是性能低下,所以不建议使用

Java代码   收藏代码
  1. /** 
  2.  * Synchronized method singleton pattern      
  3.  * @author <a href="mailto:ifuteng@gmail.com">futeng</a> 
  4.  */  
  5. public class Singleton {  
  6.   
  7.       private static Singleton singleton;  
  8.         
  9.       private Singleton() {}  
  10.         
  11.       public static synchronized Singleton getInstance() {  
  12.              if ( singleton == null) {  
  13.                    singleton = new Singleton();  
  14.             }  
  15.              return singleton;  
  16.       }  
  17. }  


  • 单例实现3:无延迟实例化式单例(Eagerly instantiaze singleton pattern)

  1. 非延迟实例化(eagerly instantiaze);线程安全(thread safe);
  2. 在类加载的第一时间被初始化,大部分场景都可胜任。丢失了延迟实例化特性,这带来的遗憾是还未被调用就已经实例化了。

Java代码   收藏代码
  1. /** 
  2.  * Eagerly singleton pattern      
  3.  * @author <a href="mailto:ifuteng@gmail.com">futeng</a> 
  4.  */  
  5. public class Singleton {  
  6.    
  7.       private static Singleton singleton = new Singleton();  
  8.         
  9.       private Singleton() {}  
  10.         
  11.       public static synchronized Singleton getInstance() {  
  12.              return singleton;  
  13.       }  
  14. }  


  • 单例实现4:双重检测加锁式单例(Double-checked locking singleton pattern)

  1. 实现延迟实例化(Lazy instantiaze);线程安全(thread safe);
  2. 最佳的单例实现,稍微复杂。

Java代码   收藏代码
  1. /** 
  2.  * Double checked singleton pattern      
  3.  * @author <a href="mailto:ifuteng@gmail.com">futeng</a> 
  4.  */  
  5. public class Singleton {  
  6.         
  7.       private volatile static Singleton singleton = new Singleton();  
  8.         
  9.       private Singleton() {}  
  10.         
  11.       public static Singleton getInstance() {  
  12.              if ( singleton == null) {  
  13.                    synchronized (Singleton. class) {  
  14.                          if ( singleton == null) {  
  15.                                singleton = new Singleton();  
  16.                         }  
  17.                   }  
  18.             }  
  19.              return singleton;  
  20.       }  
  21. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值