单例几种常见的形式

原文地址:http://forestqqqq.iteye.com/blog/1896723

一,饿汉式单例

Java代码    收藏代码
  1. //饿汉式单例1  
  2. public class Singleton1 {  
  3.     private static final Singleton1 instance = new Singleton1();  
  4.     private Singleton1(){}  
  5.     public static Singleton1 getInstance(){  
  6.         return instance;  
  7.     }  
  8. }  
  9.   
  10. //饿汉式单例2  
  11. class Singleton{  
  12.     private static class Single{  
  13.         static final Singleton instance = new Singleton();  
  14.     }  
  15.     private Singleton(){}  
  16.     public static Singleton getInstance(){  
  17.         return Single.instance;  
  18.     }  
  19. }  

 

二,懒汉式单例

Java代码    收藏代码
  1. //懒汉式单例  
  2. public class Singleton2 {  
  3.     private static Singleton2 instance = null;  
  4.     private Singleton2(){}  
  5.     public static synchronized Singleton2 getInstance(){  
  6.         if(instance == null){  
  7.             instance = new Singleton2();  
  8.         }  
  9.         return instance;  
  10.     }  
  11. }  

 

三,DCL双重锁检测式单例

Java代码    收藏代码
  1. //DCL双重锁检测式单例  
  2. public class Singleton3 {  
  3.     private volatile static Singleton3 instance = null;  
  4.     private Singleton3(){}  
  5.     public static Singleton3 getInstance(){  
  6.         if(instance == null){  
  7.             synchronized(Singleton3.class){  
  8.                 if(instance == null){  
  9.                     instance = new Singleton3();  
  10.                 }  
  11.             }  
  12.         }  
  13.         return instance;  
  14.     }  
  15. //参考文章:http://www.ibm.com/developerworks/cn/java/j-dcl.html  
  16. }  

 

 

四,登记式单例

Java代码    收藏代码
  1. import java.lang.reflect.Constructor;  
  2. import java.util.HashMap;  
  3. import java.util.Map;  
  4.   
  5. //登记式单例  
  6. public class Singleton4 {  
  7.     private  static final Map<String,Singleton4> instances =  
  8.             new HashMap<String,Singleton4>();  
  9.     static{  
  10.         Singleton4 instance = new Singleton4();  
  11.         instances.put(instance.getClass().getName(), instance);  
  12.     }  
  13.     protected Singleton4(){}  
  14.     public static synchronized Singleton4 getInstance(String name){  
  15.         if(name == null){  
  16.             name = Singleton4.class.getName();  
  17.         }  
  18.         if(instances.get(name) == null){  
  19.             try {  
  20.                 Constructor con = Class.forName(name).getDeclaredConstructor();  
  21.                 con.setAccessible(true);  
  22.                 instances.put(name, (Singleton4)con.newInstance());  
  23.             } catch (Exception e) {  
  24.                 e.printStackTrace();  
  25.             }  
  26.         }  
  27.         return instances.get(name);  
  28.     }  
  29. //参考文章:http://www.cnblogs.com/whgw/archive/2011/10/05/2199535.html  
  30. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值