zoukankan      html  css  js  c++  java
  • 设计模式---单例模式

    在JVM的整个生命周期中,采用单例模式的类,只能有一个实例

    (1)不能在类的外部随意调用类的构造器创建实例,因此,私有化构造器(private)

    (2)如何在类的外部获得类的实例,提供一个公共方法,由于类的外部不能创建类的实例,因此,该方法必须是类方法(public static SingletonClass getInstance())

    (3)在上述公共类方法中返回类的实例,由于静态方法中只能访问静态变量,因此,要返回的类的实例必须是静态变量(static)

    (1)懒汉式(线程不安全)

    public class SingletonClass {
        
        private static SingletonClass instance;
    
        private SingletonClass(){
            
        }
        public static SingletonClass getInstance(){
            if(instance == null){
                instance  = new SingletonClass();
            }
            return instance;
        }
        
    }

    (2)懒汉式(线程安全)

    public class SingletonClass {
        
        private static SingletonClass instance;
    
        private SingletonClass(){
            
        }
    /*    public synchronized static SingletonClass getInstance(){
            if(instance == null){
                instance  = new SingletonClass();
            }
            return instance;
        }*/
        public static SingletonClass getInstance(){
            synchronized (SingletonClass.class) {
                if(instance == null){
                    instance  = new SingletonClass();
                }
            }
            return instance;
        }
        
    }

    (3)饿汉式

    只要SingletonClass 类被加载了,instance就会被实例化,没有lazy loading

    public class SingletonClass {
        
        private static SingletonClass instance = new SingletonClass();
    
        private SingletonClass(){
            
        }
        public static SingletonClass getInstance(){
            return instance;
        }
    
        
    }
    public class SingletonClass {
        
        private static SingletonClass instance;
        static{
            instance =  new SingletonClass();
        }
    
        private SingletonClass(){
            
        }
        public static SingletonClass getInstance(){
            return instance;
        }
        
    }

    (4)采用静态内部类实现lazy loading

    SingletonClass 类加载的时候,不会初始化instance,之后真正调用getInstance方法时,才会初始化instance

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

    (5)使用枚举类实现单例模式

    public enum SingletonClassEnum{
        instance;
    }

    (6)双重校验锁

    public class SingletonClass {
            
        private volatile static SingletonClass instance;
        
        private SingletonClass(){
            
        }
        
        public static SingletonClass getInstance(){
            if(instance == null){
                synchronized (SingletonClass.class) {
                    if(instance == null){
                        instance  = new SingletonClass();
                    }
                }
            }
            return instance;
        }
    
        
    }
  • 相关阅读:
    容易忽视的运算符问题
    JAVA:数组,排序,查找<4>
    JAVA:类的三大特征,抽象类,接口,final关键字<3>
    JAVA:变量,数据类型,运算符,流程控制(简介)<1>
    JAVA:类,对象,成员属性,成员方法,构造方法,类变量,类方法<2>
    JAVA:二进制(原码 反码 补码),位运算,移位运算,约瑟夫问题(5)
    java静态代码块、初始化块和构造方法的执行顺序
    java面试题小全
    java中从1000万个随机数中查找出相同的10万个随机数花的最少时间
    Java数据类型总结
  • 原文地址:https://www.cnblogs.com/duanjiapingjy/p/9441436.html
Copyright © 2011-2022 走看看