首页 > 其他 > 详细

静态内部类实现单例模式

时间:2017-08-06 22:39:40      阅读:228      评论:0      收藏:0      [点我收藏+]
 1 package 设计模式.单例模式;
 2 
 3 /**
 4  * 内部类实现单例模式, 因为内部类SingletonHolder只有在getInstance()方法第一次调用的时候才会被加载(实现了lazy),
 5  * 而且其加载过程是线程安全的(实现线程安全)。 内部类加载的时候实例化一次instance。
 6  * 
 7  * @Date 2017-8-6下午9:04:04
 8  * 
 9  */
10 public class Singleton {
11     private static class SingletonHolder {
12         private static Singleton instance = new Singleton();
13     }
14 
15     private Singleton() {
16     }
17 
18     public static Singleton getInstance() {
19         return SingletonHolder.instance;
20     }
21 }

 

静态内部类实现单例模式

原文:http://www.cnblogs.com/neuhao/p/7296047.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!