• 2. 单例模式


    一.单例模式介绍

    • 某个类只能存在一个对象实例

    二.八种实现方式

    1. 饿汉式(静态常量)

    2. 饿汉式(静态代码块)

    3. 懒汉式(线程不安全)

    4. 懒汉式(线程安全,同步方法)

    5. 懒汉式(线程不安全,同步代码块)

    6. 双重检查

    7. 静态内部类

    8. 枚举

    1.饿汉式(静态常量)

    //饿汉式(静态变量)
    public class SigletonType01 {
    
        //1.构造器私有化,外部不能new
        private SigletonType01(){
    
        }
    
        //2.本类内部创建对象实例
        private final static SigletonType01 instance = new SigletonType01();
    
        //3.提供一个公有的静态方法,返回实例对象
        public static SigletonType01 getInstance(){
            return instance;
        }
    
    }

    测试:

    @Test
    public void test01(){
    
        SigletonType01 sigletonType01 = SigletonType01.getInstance();
        SigletonType01 sigletonType011 = SigletonType01.getInstance();
        System.out.println(sigletonType01 == sigletonType011);
        System.out.println(sigletonType01.hashCode());
        System.out.println(sigletonType011.hashCode());
        
        /*
        结果:
            true
            1568059495
            1568059495
        */
    }

    2.饿汉式(静态代码块)

    //饿汉式(静态代码块)
    public class SigletonType02 {
    
        //1.构造器私有化,外部不能new
        private SigletonType02(){
    
        }
    
        //2.静态代码块中创建实例
        private final static SigletonType02 instance;
        
        static{
            instance = new SigletonType02();
        }
    
        //3.提供一个公有的静态方法,返回实例对象
        public static SigletonType02 getInstance(){
            return instance;
        }
    
    }

    3.懒汉式(线程不安全)

    //懒汉式(线程不安全)
    public class SigletonType03 {
    
        //1.构造器私有化,外部不能new
        private SigletonType03(){
    
        }
    
        //2.静态代码块中创建实例
        private static SigletonType03 instance;
    
        //3.提供一个公有的静态方法,返回实例对象
        public static SigletonType03 getInstance(){
            if(instance == null){
                instance = new SigletonType03();
            }
            return instance;
        }
    
    }

    4.懒汉式(线程安全,同步方法)

    //懒汉式(线程安全,同步代码)
    public class SigletonType04 {
    
        private SigletonType04(){
    
        }
    
        private static SigletonType04 instance;
    
        //synchronized 同步代码,解决线程不安全问题
        public static synchronized SigletonType04 getInstance(){
            if(instance == null){
                instance = new SigletonType04();
            }
            return instance;
        }
    }

    5.懒汉式(线程不安全,同步代码块)

    public class SigletonType05 {
    
        private SigletonType05(){
    
        }
    
        private static SigletonType05 instance;
    
        //synchronized 同步代码块
        public static SigletonType05 getInstance(){
            if(instance == null){
                synchronized (SigletonType05.class){
                    instance = new SigletonType05();
                }
            }
            return instance;
        }
    }

    6.双重检查

    public class SigletonType06 {
    
        private SigletonType06(){
    
        }
    
        private static volatile SigletonType06 instance;
        
        public static SigletonType06 getInstance(){
            if(instance == null){
                synchronized (SigletonType06.class){
                    if(instance == null){
                        instance = new SigletonType06();
                    }
                }
            }
            return instance;
        }
    }

    7.静态内部类

    public class SigletonType07 {
    
        private SigletonType07(){
    
        }
    
        public static class SigletonInstance{
            private static final SigletonType07 instance = new SigletonType07();
        }
    
        public static SigletonType07 getInstance(){
            return SigletonInstance.instance;
        }
    }

    8.枚举

    enum  SigletonType08 {
    
        INSTANCE;
    
        public void method(){
            System.out.println("method");
        }
    }

    三.JDK中使用单例模式例子

     四.单例模式注意事项及细节说明

  • 相关阅读:
    Spring学习,初识Spring
    Spring学习整理
    表单验证一些思考
    为什么要使用mybaits
    JDBC缺点分析
    Java 学习笔记提高篇
    Java基础学习笔记(四)
    Java基础学习笔记(三)
    Java学习笔记(二)
    centos7运行级别和图形界面相关操作
  • 原文地址:https://www.cnblogs.com/zhihaospace/p/12452647.html
Copyright © 2020-2023  润新知