zoukankan      html  css  js  c++  java
  • 【剑指offer】面试题 2. 实现 Singleton 模式

    面试题 2. 实现 Singleton 模式

    题目:设计一个类,我们只能生成该类的一个实例。
    单例模式:确保一个类只有一个实例,并提供了一个全局访问点。

    Java 实现

    1.饿汉模式

    //饿汉模式
    public class Singleton1 {
        //私有化构造方法
        private Singleton1() {
        }
    
        private static final Singleton1 instance = new Singleton1();
    
        public static Singleton1 getInstance1() {
            return instance;
        }
    }
    

    2.饿汉模式、变种

    //饿汉模式、变种
    public class Singleton2 {
        //私有化构造方法
        private Singleton2() {
        }
    
        private static final Singleton2 instance;
    
        static {
            instance = new Singleton2();
        }
    
        public static Singleton2 getInstance2() {
            return instance;
        }
    }
    

    3.懒汉、线程不安全

    //懒汉、线程不安全
    public class Singleton3 {
        //私有化构造方法
        private Singleton3() {
        }
    
        private static volatile Singleton3 instance = null;
    
        public static Singleton3 getInstance3() {
            if (instance == null) {
                instance = new Singleton3();
            }
            return instance;
        }
    }
    

    4.懒汉、线程安全

    //懒汉、线程安全
    public class Singleton4 {
        //私有化构造方法
        private Singleton4() {
        }
    
        private static volatile Singleton4 instance = null;
    
        public static synchronized Singleton4 getInstance4() {
            if (instance == null) {
                instance = new Singleton4();
            }
            return instance;
        }
    }
    

    5.静态内部类

    //静态内部类
    public class Singleton5 {
        //私有化构造方法
        private Singleton5() {
        }
    
        //静态内部类
        private static final class SingletonHandler {
            private static final Singleton5 INSTANCE = new Singleton5();
        }
    
        public static Singleton5 getInstance5() {
            return SingletonHandler.INSTANCE;
        }
    }
    

    6.枚举

    //枚举
    public enum Singleton6 {
        INSTANCE;
    
        public void whateverMethod() {
    
        }
    }
    

    7.双重校验锁

    //双重校验锁
    public class Singleton7 {
        //私有化构造方法
        private Singleton7() {
        }
    
        private static volatile Singleton7 instance = null;
    
        public static Singleton7 getInstance7() {
            if (instance == null) {
                synchronized (Singleton7.class) {
                    if (instance == null) {
                        instance = new Singleton7();
                    }
                }
            }
            return instance;
        }
    }
    
  • 相关阅读:
    ① ts基础
    ⑦ 原型和原型链 作用域链
    ④ 小程序使用分包
    功能⑦ 小程序整合高德地图定位
    effective OC2.0 52阅读笔记(三 接口与API设计)
    effective OC2.0 52阅读笔记(二 对象、消息、运行期)
    effective OC2.0 52阅读笔记(一 熟悉Objective-C)
    perl的Getopt::Long和pod::usage ?
    安装你自己的perl modules
    Perl 之 use(), require(), do(), %INC and @INC
  • 原文地址:https://www.cnblogs.com/hgnulb/p/9026546.html
Copyright © 2011-2022 走看看