zoukankan      html  css  js  c++  java
  • 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中使用单例模式例子

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

  • 相关阅读:
    sql server模糊查询、分组
    glup watch reload 保存 刷新 原理
    a daemon 守护进程 shell命令行以&结尾
    单道程序 多道程序
    10.6 Comment Syntax
    Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file.
    在常见的机器学习/深度学习项目里,数据准备占去整个分析管道的60%到80%。
    算法所产生的性能改进已经超过了硬件所带来的性能提升 The future is algorithms, not code
    postgresql_action
    InnoDB Crash Recovery InnoDB崩溃恢复
  • 原文地址:https://www.cnblogs.com/zhihaospace/p/12452647.html
Copyright © 2011-2022 走看看