单例模式

 1 class Singleton{
 2     // 1:私有化构造方法
 3     private Singleton(){};
 4 
 5     // 2:私有的静态内部类,类加载器负责加锁
 6     private static class SingletonHolder{
 7         private static Singleton singleton = new Singleton();
 8     }
 9 
10     // 3:自行对外提供实例
11     public static Singleton getInstance(){
12         return SingletonHolder.singleton;
13     }
14 }

 

posted @ 2020-05-14 15:41  〆平平淡淡才是真  阅读(79)  评论(0编辑  收藏  举报