【剑指offer】面试题 2. 实现 Singleton模式

面试题 2. 实现 Singleton模式

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

Code

  • 1.饿汉模式

    //饿汉模式
    public class Singleton1 {
        //私有化构造方法
        private Singleton1(){}
        private static Singleton1 instance = new Singleton1();
        public static Singleton1 getInstance1(){
            return instance;
        }
    }
  • 2.饿汉模式、变种
    //饿汉模式、变种
    public class Singleton2 {
        //私有化构造方法
        private Singleton2(){}
        private static  Singleton2 instance = null;
        static{
            instance = new Singleton2();
        }
        public static Singleton2 getInstance2(){
            return instance;
        }
    }
  • 3.懒汉、线程不安全
    //懒汉、线程不安全
    public class Singleton3 {
        //私有化构造方法
        private Singleton3(){}
        private static Singleton3 instance = null;
        public static Singleton3 getInstance3(){
            if(instance==null){
                instance = new Singleton3();
            }
            return instance;
        }
    }
  • 4.懒汉、线程安全
    //懒汉、线程安全
    public class Singleton4 {
        //私有化构造方法
        private Singleton4(){}
        private static Singleton4 instance;
        public static synchronized Singleton4 getInstance4(){
            if(instance==null){
                instance = new Singleton4();
            }
            return instance;
        }
    }
  • 5.静态内部类
    //静态内部类
    public class Singleton5 {
        //私有化构造方法
        private Singleton5(){}
        //静态内部类
        private static 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 volatile static Singleton7 instance;
        public static Singleton7 getInstance7(){
            if(instance==null){
                synchronized(Singleton7.class){
                    if(instance==null){
                        instance = new Singleton7();
                    }
                }
            }
            return instance;
        }
    }
  • 测试类
    public class Test {
        public static void main(String[] args) {
            Singleton1 instance1 = Singleton1.getInstance1();
            Singleton1 instance2 = Singleton1.getInstance1();
            System.out.println("单例模式一:"+(instance1==instance2));
    
            Singleton2 instance3 = Singleton2.getInstance2();
            Singleton2 instance4 = Singleton2.getInstance2();
            System.out.println("单例模式二:"+(instance3==instance4));
    
            Singleton3 instance5 = Singleton3.getInstance3();
            Singleton3 instance6 = Singleton3.getInstance3();
            System.out.println("单例模式三:"+(instance5==instance6));
    
            Singleton4 instance7 = Singleton4.getInstance4();
            Singleton4 instance8 = Singleton4.getInstance4();
            System.out.println("单例模式四:"+(instance7==instance8));
    
            Singleton5 instance9 = Singleton5.getInstance5();
            Singleton5 instance10 = Singleton5.getInstance5();
            System.out.println("单例模式五:"+(instance9==instance10));
    
            Singleton6 instanceOne = Singleton6.INSTANCE;
            Singleton6 instanceTwo = Singleton6.INSTANCE;
            System.out.println("单例模式五:"+(instanceOne==instanceTwo));
    
            Singleton7 instance11 = Singleton7.getInstance7();
            Singleton7 instance12 = Singleton7.getInstance7();
            System.out.println("单例模式七:"+(instance11==instance12));
        }
    }

    结果

    单例模式一:true
    单例模式二:true
    单例模式三:true
    单例模式四:true
    单例模式五:true
    单例模式五:true
    单例模式七:true

原文地址:https://www.cnblogs.com/hglibin/p/9026546.html

时间: 2024-08-01 22:11:56

【剑指offer】面试题 2. 实现 Singleton模式的相关文章

【剑指Offer面试题】二维数组中的查找

下决心AC所有剑指offer面试题. 九度OJ面试题地址:http://ac.jobdu.com/hhtproblems.php 书籍:何海涛--<剑指Offer:名企面试官精讲典型编程题> 对于面试题,面试官往往更希望我们能提出优化方法,这样更能体现我们的思维能力以及传说中的"内功".所以做剑指offer要着重训练这方面,多总结多细究,总是有好处的.加油~ 二维数组中的查找 时间限制:1 秒内存限制:32 兆 特殊判题:否提交:19005解决:3642 题目描述: 在一个

【剑指Offer面试题】九度OJ1384:二维数组中的查找

下决心AC全部剑指offer面试题. 九度OJ面试题地址:http://ac.jobdu.com/hhtproblems.php 书籍:何海涛--<剑指Offer:名企面试官精讲典型编程题> 对于面试题,面试官往往更希望我们能提出优化方法,这样更能体现我们的思维能力以及传说中的"内功".所以做剑指offer要着重训练这方面,多总结多细究,总是有优点的.加油~ 题目链接地址: http://ac.jobdu.com/problem.php?pid=1384 二维数组中的查找

【剑指Offer面试题】 九度OJ1516:调整数组顺序使奇数位于偶数前面

题目链接地址: http://ac.jobdu.com/problem.php?pid=1516 题目1516:调整数组顺序使奇数位于偶数前面 时间限制:1 秒内存限制:128 兆特殊判题:否提交:2858解决:924 题目描写叙述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得全部的奇数位于数组的前半部分,全部的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 输入: 每一个输入文件包括一组測试案例. 对于每一个測试案例.第一行输入一个n,代表该数组

【剑指Offer面试题】 九度OJ1386:旋转数组的最小数字

题目链接地址: http://ac.jobdu.com/problem.php?pid=1386 题目1386:旋转数组的最小数字 时间限制:1 秒内存限制:32 兆特殊判题:否提交:6914解决:1534 题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1. 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为

剑指Offer 面试题36:数组中的逆序对及其变形(Leetcode 315. Count of Smaller Numbers After Self)题解

剑指Offer 面试题36:数组中的逆序对 题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 例如, 在数组{7,5,6,4}中,一共存在5个逆序对,分别是(7,6),(7,5),(7,4),(6,4)和(5,4),输出5. 提交网址: http://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5?tpId=13&tqId=11188 或 htt

二叉树层次遍历(剑指Offer面试题32:从上到下打印二叉树)

图1所示为二叉树的层次遍历,即按照箭头所指方向,按照1.2.3的层次顺序,对二叉树每个节点进行访问 (此图反映的是自左至右的层次遍历,自右至左的方式类似). 要进行层次遍历,需要建立一个队列.先将二叉树头节点入队列,然后出队列,访问该节点, 如果它有左子树,则将左子树的根结点入队:如果它有右子树,则将右子树的根结点入队.然后出队列,对出队节点访问, 如此反复直到队列为空为止. 1 import java.util.*; 2 class TreeNode 3 { 4 int val; 5 Tree

[ 剑指offer ] 面试题8:二叉树的下一个节点

题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 解题思路 1.找到所有的可能情况并归纳,写的代码需要把这些情况都覆盖到. 2.具体情况详见书本# -*- coding:utf-8 -*- # class TreeLinkNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # self

剑指offer面试题29:数组中出现次数超过一半的数字

题目:数组中有一个数字出现的次数超过数组长度的一般,请找出这个数字,例如输入一个长度为9的数组(1,2,3,2,2,2,5,4,2,).由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. 个人第一眼想法是通过一个sort函数,再判断中间那数出现次数,只要出现多于n/2,就直接输出. 一般来说,最为直观的算法面试官都不会满意,那么有没有更优的算法呢? 这种算法是受快速排序算法的启发.在随机快速排序算法中,我们现在数组中随机选择一个数字,然后调整数组中数字的顺序,使得比选中的数字小的数字

剑指offer 面试题8:旋转数组的最小数字 题解

面试题8:旋转数组的最小数字 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个已从小到大排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1.(要求不能直接遍历数组来求解.) 提交网址: http://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?tpId=13&tqId=11159 或 http:

剑指offer 面试题33 把数组排成最小的数

题目链接: 剑指offer 题目链接: 把数组排成最小的数, 例如{3, 32, 321} 输出: 321323 解题思路: 我现在已经知道正确答案了, 就是没有办法去证明, 先去开会, 在开会的时候再去想. 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator&g