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

单例模式8种写法

时间:2020-07-10 19:09:58      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:nbsp   对象   同步代码块   也会   pre   static   gets   bsp   pattern   

 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种写法

标签:nbsp   对象   同步代码块   也会   pre   static   gets   bsp   pattern   

原文地址:https://www.cnblogs.com/java-leaner/p/13280555.html

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