单例模式




 本文章不针对任何项目,只是在此做个小结。

 

Java有三种单例模式的实现方法:饿汉式、懒汉式和登记式。

1、饿汉式单例类:

Java代码    收藏代码
  1. public class TestSingleton1 {  
  2.       
  3.     private TestSingleton1(){  
  4.     }  
  5.       
  6.     //自行实例化  
  7.     private static  TestSingleton1 instance = new TestSingleton1();   
  8.       
  9.     //静态工厂方法  
  10.     public static TestSingleton1 getInstance(){  
  11.         return instance;  
  12.     }  
  13. }  

 

个人感觉:饿汉式单例,使用资源多但速度快,如果用的类不是很大的话,饿汉式单例方法是比较好的。

 

2、懒汉式单例类:

Java代码    收藏代码
  1. public class TestSingleton2 {  
  2.       
  3.     private TestSingleton2(){  
  4.     }  
  5.       
  6.     //无final  
  7.     private static TestSingleton2 instance = null;  
  8.       
  9.     //静态工厂方法,加上synchronized同步的话,比较耗内存,可以参考双重检查加锁方法,如是单线程,无需同步  
  10.     public synchronized static TestSingleton2 getInstance(){  
  11.         if (instance == null ) {  
  12.             instance = new TestSingleton2();  
  13.         }  
  14.           
  15.         return instance;  
  16.     }  
  17. }  

懒汉式单例类是对饿汉式单例类的一个性能优化。

在懒汉式单例类中,如果类没有被使用,就不会再创建类的实例了,从而减少内存的消耗。

 

个人感觉:懒汉式单例类是一种延时加载,反应时间上变长了。它要考虑多个线程首次引用单例类时的访问问题,加上synchronized同步的话,比较耗内存,为解决此种情况,就加锁,加锁以后,就只能在一个线程创建完对象后另一个线程才能使用这个类,保证了类的单一性,当然也可以使用双重检查加锁double-check。

双重检查加锁double-check:

Java代码    收藏代码
  1. public class TestSingleton21 {  
  2.       
  3.     private volatile static TestSingleton21 Instance;  
  4.       
  5.     private TestSingleton21(){  
  6.     }  
  7.       
  8.     //双重检查加锁  
  9.     public static TestSingleton21 getInstance(){  
  10.         if (Instance == null) {  
  11.             synchronized (TestSingleton21.class) {  
  12.                 if (Instance == null) {  
  13.                     Instance = new TestSingleton21();  
  14.                 }  
  15.             }  
  16.         }  
  17.           
  18.         return Instance;  
  19.     }  
  20. }  

 不过,double-check 也是不安全的。

 有一种比较流行的静态内部类持有外部类的实例,使用jvm的类载入机制保证单例模式在多线程环境下的正确加载:

Java代码    收藏代码
  1. public class TestSingleton22 {  
  2.       
  3.     private TestSingleton22(){  
  4.     }  
  5.       
  6.     //静态内部类持有外部类的实例  
  7.     public static TestSingleton22 getInstance(){  
  8.         return TestInstance.getInstance();  
  9.     }  
  10.       
  11.     //私有类  
  12.     private static class TestInstance{  
  13.         private static TestSingleton22 instance = new TestSingleton22();  
  14.           
  15.         private TestInstance(){  
  16.         }  
  17.           
  18.         private static TestSingleton22 getInstance(){  
  19.             return instance;  
  20.         }  
  21.     }  
  22. }  

 

只能说,这种单例模式现在比较流行,但不是什么情况下使用它都最适合,还要看具体情况。

3、登记式单例类:

Java代码    收藏代码
  1. public class TestSingleton3 {  
  2.   
  3.     private static Map<String, TestSingleton3> map = new HashMap<String, TestSingleton3>();  
  4.   
  5.     static {  
  6.         TestSingleton3 instance = new TestSingleton3();  
  7.         map.put(instance.getClass().getName(), instance);//注册类名  
  8.     }  
  9.   
  10.     // 保护默认的构造器  
  11.     protected TestSingleton3() {  
  12.     }  
  13.   
  14.     public static TestSingleton3 getInstance(String name) {  
  15.         if (name == null) {  
  16.             name = TestSingleton3.class.getName();  
  17.             System.out.println("name: " + name);  
  18.         }  
  19.         if (map.get(name) == null) {  
  20.             try {  
  21.                 map.put(name, (TestSingleton3) Class.forName(name)  
  22.                         .newInstance());  
  23.             } catch (InstantiationException e) {  
  24.                 e.printStackTrace();  
  25.             } catch (IllegalAccessException e) {  
  26.                 e.printStackTrace();  
  27.             } catch (ClassNotFoundException e) {  
  28.                 e.printStackTrace();  
  29.             }  
  30.         }  
  31.   
  32.         return map.get(name);  
  33.     }  
  34.   
  35.     private String say() {  
  36.         return "演讲开始。。。";  
  37.     }  
  38.   
  39.     public static void main(String[] args) {  
  40.         TestSingleton3 instance = TestSingleton3.getInstance(null);  
  41.         System.out.println(instance.say());  
  42.     }  
  43.   
  44. }  

此种方法是对一组单例类的管理和维护,实例全部被放进map中,对登记过的直接返回,对没登记过的登记注册再返回。是一种整体思路。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值