美文网首页Java 程序员Java
丧心病狂!面试官让我介绍:单例模式的使用场景及"12种"写法

丧心病狂!面试官让我介绍:单例模式的使用场景及"12种"写法

作者: 马小莫QAQ | 来源:发表于2021-09-10 17:30 被阅读0次

    1. 什么是单例模式

    单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

    这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

    保证一个类仅有一个实例,并提供一个访问它的全局访问点。

    2. 单例模式的优点/缺点和使用场景

    2.1 单例模式的优点

    • 提供了对唯一实例的受控访问
    • 由于系统中内存只存在一个对象,因此可以节约系统的的资源,对于一些频繁的创建和销毁的对象单例模式无疑可以提高系统的性能
    • 单例模式可以允许可变的数目的实例,使用单例模式进行扩展,使用控制单利对象相似的方法获取指定个数的实例,及解决了单利对象,共享过多,而有损性能的问题

    2.2 单例模式的缺点

    • 由于单例模式,不是抽象的所以可扩展性比较差
    • 职责过重,在一定程度上违背了单一职责
    • 滥用单例将带来一些负面的问题,如为了节省资源将数据库连接池对象设计为单例模式,可能会导致共享连接池对象的程序过多未出而出现的连接池溢出,如果实例化对象长时间不用系统就会被认为垃圾对象被回收,这将导致对象状态丢失

    2.3 单例模式的使用场景

    开发工具类库中的很多工具类都应用了单例模式,比例线程池、缓存、日志对象等,它们都只需要创建一个对象。

    3. 单例模式的12种写法

    3.1 饿汉式(静态变量)

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

    3.2 饿汉式(静态常量)

    public class Singleton {
        private final static Singleton INSTANCE = new Singleton();
    
        private Singleton() {
        }
    
        public static Singleton getInstance() {
            return INSTANCE;
        }
    }
    

    3.3 饿汉式(静态代码块)

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

    上面三种写法本质上其实是一样的,也是各类文章在介绍饿汉式时常用的方式。但使用静态final的实例对象或者使用静态代码块依旧不能解决在反序列化、反射、克隆时重新生成实例对象的问题。

    序列化:一是可以将一个单例的实例对象写到磁盘,实现数据的持久化;二是实现对象数据的远程传输。

    当单例对象有必要实现 Serializable 接口时,即使将其构造函数设为私有,在它反序列化时依然会通过特殊的途径再创建类的一个新的实例,相当于调用了该类的构造函数有效地获得了一个新实例!

    3.4 懒汉式(线程不安全)

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

    3.5 懒汉式(线程安全,存在同步开销)

    class Singleton {
        private static Singleton intance = null;
    
        private Singleton() {
            //私有构造函数
        }
    
        public static synchronized Singleton getInstance()
        {
            if (intance == null) {
                intance = new Singleton();
            }
            return intance;
        }
    }
    

    3.6 懒汉式(线程假装安全,同步代码块)

    class Singleton {
    
        private static Singleton singleton;
    
        private Singleton() {
    
        }
        public static Singleton getInstance() {
    
            if (singleton == null) {
                // 4.加入同步代码块
                synchronized (Singleton.class) {
                    singleton = new Singleton();
                }
            }
            return singleton;
        }
    }
    

    3.7 DCL「双重检测锁:Double Checked Lock」(假)

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

    3.8 DCL「双重检测锁:Double Checked Lock」 单例(真)

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

    3.9 静态内部类(推荐使用)

    public class Singleton {
        private Singleton() {
        }
    
        private static class SingletonInstance()
    
        {
            private static final Singleton INSTANCE = new Singleton();
        }
    
        public static Singleton getInstance() {
            return SingletonInstance.INSTANCE;
        }
    }
    

    3.10 枚举类单例模式

    public enum Singleton {
        INSTANCE;
        private Resource instance;
    
        Singleton() {
            instance = new Resource();
        }
    
        public Resource getInstance() {
            return instance;
        }
    
        public static class Resource {
            private Resource() {
    
            }
        }
    }
    

    3.11 登记式单例--使用Map容器来管理单例模式

    public class Singleton {
        private static Map<String, Object> map = new HashMap<>();
    
        public static void reglisterService(String key, Object instance) {
            if (!map.containsKey) {
                map.put(key, instance);
            }
        }
    
        public static Object getInstance(String key) {
            return map.get(key);
        }
    }
    

    3.12 内部枚举类

    public interface MySingleton {
        void doSomething();
    }
    
    public enum Singleton implements MySingleton {
        INSTANCE {
            @Override
            public void doSomething() {
                System.out.println("complete singleton");
            }
        };
    
        public static MySingleton getInstance() {
            return Singleton.INSTANCE;
        }
    }
    

    4.Spring依赖注入对单例的使用

    在AbstractBeanFactory中

    @Override
        public Object getBean(String name) throws BeansException {
            return doGetBean(name, null, null, false);
        }
    
        @Override
        public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
            return doGetBean(name, requiredType, null, false);
        }
    
        @Override
        public Object getBean(String name, Object... args) throws BeansException {
            return doGetBean(name, null, args, false);
        }
    

    4.1 doGetBean中getSingleton

    protected <T> T doGetBean(
                String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
                throws BeansException {
    
            String beanName = transformedBeanName(name);
            Object bean;
    
            // Eagerly check singleton cache for manually registered singletons.
            Object sharedInstance = getSingleton(beanName);
            if (sharedInstance != null && args == null) {
                if (logger.isTraceEnabled()) {
                    if (isSingletonCurrentlyInCreation(beanName)) {
                        logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
                                "' that is not fully initialized yet - a consequence of a circular reference");
                    }
                    else {
                        logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
                    }
                }
                bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
            }
    
            else {
                // Fail if we're already creating this bean instance:
                // We're assumably within a circular reference.
                if (isPrototypeCurrentlyInCreation(beanName)) {
                    throw new BeanCurrentlyInCreationException(beanName);
                }
    
                // Check if bean definition exists in this factory.
                BeanFactory parentBeanFactory = getParentBeanFactory();
                if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                    // Not found -> check parent.
                    String nameToLookup = originalBeanName(name);
                    if (parentBeanFactory instanceof AbstractBeanFactory) {
                        return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
                                nameToLookup, requiredType, args, typeCheckOnly);
                    }
                    else if (args != null) {
                        // Delegation to parent with explicit args.
                        return (T) parentBeanFactory.getBean(nameToLookup, args);
                    }
                    else if (requiredType != null) {
                        // No args -> delegate to standard getBean method.
                        return parentBeanFactory.getBean(nameToLookup, requiredType);
                    }
                    else {
                        return (T) parentBeanFactory.getBean(nameToLookup);
                    }
                }
    
                if (!typeCheckOnly) {
                    markBeanAsCreated(beanName);
                }
    
                StartupStep beanCreation = this.applicationStartup.start("spring.beans.instantiate")
                        .tag("beanName", name);
                try {
                    if (requiredType != null) {
                        beanCreation.tag("beanType", requiredType::toString);
                    }
                    RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                    checkMergedBeanDefinition(mbd, beanName, args);
    
                    // Guarantee initialization of beans that the current bean depends on.
                    String[] dependsOn = mbd.getDependsOn();
                    if (dependsOn != null) {
                        for (String dep : dependsOn) {
                            if (isDependent(beanName, dep)) {
                                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                        "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
                            }
                            registerDependentBean(dep, beanName);
                            try {
                                getBean(dep);
                            }
                            catch (NoSuchBeanDefinitionException ex) {
                                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                        "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
                            }
                        }
                    }
    
                    // Create bean instance.
                    if (mbd.isSingleton()) {
                        sharedInstance = getSingleton(beanName, () -> {
                            try {
                                return createBean(beanName, mbd, args);
                            }
                            catch (BeansException ex) {
                                // Explicitly remove instance from singleton cache: It might have been put there
                                // eagerly by the creation process, to allow for circular reference resolution.
                                // Also remove any beans that received a temporary reference to the bean.
                                destroySingleton(beanName);
                                throw ex;
                            }
                        });
                        bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                    }
    
                    else if (mbd.isPrototype()) {
                        // It's a prototype -> create a new instance.
                        Object prototypeInstance = null;
                        try {
                            beforePrototypeCreation(beanName);
                            prototypeInstance = createBean(beanName, mbd, args);
                        }
                        finally {
                            afterPrototypeCreation(beanName);
                        }
                        bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
                    }
    
                    else {
                        String scopeName = mbd.getScope();
                        if (!StringUtils.hasLength(scopeName)) {
                            throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
                        }
                        Scope scope = this.scopes.get(scopeName);
                        if (scope == null) {
                            throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
                        }
                        try {
                            Object scopedInstance = scope.get(beanName, () -> {
                                beforePrototypeCreation(beanName);
                                try {
                                    return createBean(beanName, mbd, args);
                                }
                                finally {
                                    afterPrototypeCreation(beanName);
                                }
                            });
                            bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                        }
                        catch (IllegalStateException ex) {
                            throw new ScopeNotActiveException(beanName, scopeName, ex);
                        }
                    }
                }
                catch (BeansException ex) {
                    beanCreation.tag("exception", ex.getClass().toString());
                    beanCreation.tag("message", String.valueOf(ex.getMessage()));
                    cleanupAfterBeanCreationFailure(beanName);
                    throw ex;
                }
                finally {
                    beanCreation.end();
                }
            }
    
            // Check if required type matches the type of the actual bean instance.
            if (requiredType != null && !requiredType.isInstance(bean)) {
                try {
                    T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
                    if (convertedBean == null) {
                        throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
                    }
                    return convertedBean;
                }
                catch (TypeMismatchException ex) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Failed to convert bean '" + name + "' to required type '" +
                                ClassUtils.getQualifiedName(requiredType) + "'", ex);
                    }
                    throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
                }
            }
            return (T) bean;
        }
    

    4.2 getSingleton的实现

    返回在给定名称下注册的(原始)单例对象,检查已经实例化的单例并允许提前 对当前创建的单例的引用(解析循环引用)。

    public Object getSingleton(String beanName) {
            return getSingleton(beanName, true);
        }
    @Nullable
        protected Object getSingleton(String beanName, boolean allowEarlyReference) {
            // Quick check for existing instance without full singleton lock
            Object singletonObject = this.singletonObjects.get(beanName);
            if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
                singletonObject = this.earlySingletonObjects.get(beanName);
                if (singletonObject == null && allowEarlyReference) {
                    synchronized (this.singletonObjects) {
                        // Consistent creation of early reference within full singleton lock
                        singletonObject = this.singletonObjects.get(beanName);
                        if (singletonObject == null) {
                            singletonObject = this.earlySingletonObjects.get(beanName);
                            if (singletonObject == null) {
                                ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                                if (singletonFactory != null) {
                                    singletonObject = singletonFactory.getObject();
                                    this.earlySingletonObjects.put(beanName, singletonObject);
                                    this.singletonFactories.remove(beanName);
                                }
                            }
                        }
                    }
                }
            }
            return singletonObject;
        }
    

    作者:ZhangSan_Plus
    链接:https://juejin.cn/post/7006124074338369567
    来源:掘金

    相关文章

      网友评论

        本文标题:丧心病狂!面试官让我介绍:单例模式的使用场景及"12种"写法

        本文链接:https://www.haomeiwen.com/subject/nfdnwltx.html