单例模式8种写法

Posted java-leaner

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单例模式8种写法相关的知识,希望对你有一定的参考价值。

 1 package com.learn.design_pattern.singleton;
 2 
 3 /**
 4  * TODO
 5  *
 6  * @author chip
 7  * @version 1.0
 8  * @date 2020/7/10 16:59
 9  * 饿汉模式第一种(静态常量)
10  *
11  */
12 public class Singleton1 {
13 
14     private Singleton1(){}
15 
16     private static final Singleton1 singleton1=new Singleton1();
17 
18     public static Singleton1 getSingleton1(){
19         return singleton1;
20     }
21 }
 1 package com.learn.design_pattern.singleton;
 2 
 3 /**
 4  * TODO
 5  *
 6  * @author chip
 7  * @version 1.0
 8  * @date 2020/7/10 17:01
 9  *  饿汉模式第二种(静态代码块)
10  */
11 public class Singleton2 {
12     private Singleton2(){}
13 
14     private static Singleton2 singleton2;
15 
16     static {
17         singleton2=new Singleton2();
18     }
19 
20     public static Singleton2 getSingleton2(){
21         return singleton2;
22     }
23 }
 1 package com.learn.design_pattern.singleton;
 2 
 3 /**
 4  * TODO
 5  *
 6  * @author chip
 7  * @version 1.0
 8  * @date 2020/7/10 17:04
 9  * 懒汉模式第一种
10  */
11 public class Singleton3 {
12     private Singleton3(){}
13 
14     private static Singleton3 singleton3;
15 
16     /**
17      *
18      *  
19      * @author chip
20      * @date 2020/7/10 17:14 
21      * @return com.learn.design_pattern.singleton.Singleton3
22      * 两个线程同时通过if判断的情况下,线程不安全
23      */
24     public static Singleton3 singleton3(){
25         if(singleton3==null){
26             singleton3=new Singleton3();
27         }
28         return singleton3;
29     }
30 }
 1 package com.learn.design_pattern.singleton;
 2 
 3 /**
 4  * TODO
 5  *
 6  * @author chip
 7  * @version 1.0
 8  * @date 2020/7/10 17:08
 9  * 懒汉式第二种
10  */
11 public class Singleton4 {
12     private Singleton4(){}
13 
14     private static Singleton4 singleton4;
15 
16     /**
17      *
18      *  
19      * @author chip
20      * @date 2020/7/10 17:15
21      * @return com.learn.design_pattern.singleton.Singleton4
22      * if判断只需进行一次就可以,之后一直锁住方法影响效率
23      */
24     public static synchronized Singleton4 getSingleton4(){
25         if(singleton4==null){
26             singleton4=new Singleton4();
27         }
28         return singleton4;
29     }
30 }
 1 package com.learn.design_pattern.singleton;
 2 
 3 /**
 4  * TODO
 5  *
 6  * @author chip
 7  * @version 1.0
 8  * @date 2020/7/10 17:17
 9  * 懒汉模式第三种
10  */
11 public class Singleton5 {
12     private Singleton5(){}
13 
14     private static Singleton5 singleton5;
15 
16     /**
17      *
18      *  
19      * @author chip
20      * @date 2020/7/10 17:19
21      * @return com.learn.design_pattern.singleton.Singleton5
22      * 两个线程同时通过if判断,即使上锁也会创建两个不同的对象,线程不安全
23      */
24     public static Singleton5 getSingleton5(){
25         if(singleton5==null){
26             synchronized (Singleton5.class){
27                 singleton5=new Singleton5();
28             }
29         }
30         return singleton5;
31     }
32 }
 1 package com.learn.design_pattern.singleton;
 2 
 3 /**
 4  * TODO
 5  *
 6  * @author chip
 7  * @version 1.0
 8  * @date 2020/7/10 17:20
 9  * 双重锁校验
10  */
11 public class Singleton6 {
12     private Singleton6(){}
13 
14     private static volatile Singleton6 singleton6;
15     
16     /**
17      *
18      *  
19      * @author chip
20      * @date 2020/7/10 17:23 
21      * @return com.learn.design_pattern.singleton.Singleton6
22      * 第一个if进入后锁住对象,再进行判断,即使在多线程情况下也可保证线程同步,并且同步代码块之后都不会执行,提高效率
23      */
24     public static Singleton6 getSingleton6(){
25         if(singleton6==null){
26             synchronized (Singleton6.class){
27                 if(singleton6==null){
28                     singleton6=new Singleton6();
29                 }
30             }
31         }
32         return singleton6;
33     }
34 }
 1 package com.learn.design_pattern.singleton;
 2 
 3 /**
 4  * TODO
 5  *
 6  * @author chip
 7  * @version 1.0
 8  * @date 2020/7/10 17:25
 9  * 静态内部类
10  */
11 public class Singleton7 {
12     private Singleton7(){}
13 
14 
15     private static class SingletonClass{
16         private static final Singleton7 INSTANCE=new Singleton7();
17     }  
18     
19     /**
20      *
21      *  
22      * @author chip
23      * @date 2020/7/10 17:30 
24      * @return com.learn.design_pattern.singleton.Singleton7
25      * 外部类装载化时不会装载静态内部类
26      * 调用方法时才会装载静态内部类,而装载静态内部类是jdk实现的,线程安全的
27      */
28     public static Singleton7 getSingleton7(){
29         return SingletonClass.INSTANCE;
30     }
31 }
 1 package com.learn.design_pattern.singleton;
 2 
 3 /**
 4  * TODO
 5  *
 6  * @author chip
 7  * @version 1.0
 8  * @date 2020/7/10 17:29
 9  * 枚举
10  */
11 enum  Singleton8 {
12     INSTANCE;
13 }

 

以上是关于单例模式8种写法的主要内容,如果未能解决你的问题,请参考以下文章

单例模式8种写法

详解单例模式8种写法-一站式搞定面试官

性能比较好的单例写法

单例模式的 8 种写法,非常全!

java设计模式 - 单例模式(干货)

面试中单例模式有几种写法?