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

静态内部类实现单例模式

时间:2017-08-06 23:13:22      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:单例   single   实现   加载过程   new   span   ati   方法   instance   

 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 }

 

静态内部类实现单例模式

标签:单例   single   实现   加载过程   new   span   ati   方法   instance   

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

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