面试题——单例模式

  • 单例模式的五种实现方式。
  • 了解JDK中哪些地方体现了单例模式。
    • Runtime类(饿汉式)、System类(双检锁-懒汉式)、Collections工具类。可到idea中查看源码。

饿汉式

package com.jie.singleton;

import java.io.Serializable;

/**
 * @Author 小杰
 * @Date 2022/5/16 15:10
 * @Version 1.0
 * 饿汉式
 */
public class Singleton1 implements Serializable {
    private Singleton1(){
        if (INSTANCE!=null){
            throw new RuntimeException("单例模式不能重复创建对象");
        }
        System.out.println("private Singleton1()...");
    }
    private static final Singleton1 INSTANCE=new Singleton1();
    public static Singleton1 getInstance(){
        return INSTANCE;
    }
    public static void otherMethod(){
        System.out.println("otherMethod()...");
    }
    public Object readResolve(){
        return INSTANCE;
    }
}

package com.jie.singleton;

/**
 * @Author 小杰
 * @Date 2022/5/16 15:34
 * @Version 1.0
 * 枚举饿汉式。
 */
public enum Singleton2 {
    /**
     * 单例对象
     */
    INSTANCE;

    Singleton2() {
        System.out.println("Singleton2 ...");
    }

    public static Singleton2 getInstance() {
        return INSTANCE;
    }
    public static void otherMethod(){
        System.out.println("otherMethod()...");
    }

    @Override
    public String toString() {
        return getClass().getName()+"@"+Integer.toHexString(hashCode());
    }
}

懒汉式

package com.jie.singleton;

/**
 * @Author 小杰
 * @Date 2022/5/16 15:46
 * @Version 1.0
 * 懒汉式单例
 */
public class Singleton3 {
    private static  Singleton3 INSTANCE=null;
    private Singleton3(){
        if (INSTANCE!=null){
            throw new RuntimeException("单例模式不能重复创建对象");
        }
        System.out.println("private Singleton1()...");
    }

    /**
     * 加synchronized解决线程安全问题。
     * @return
     */
    public static synchronized Singleton3 getInstance(){
        if (INSTANCE==null){
            INSTANCE=new Singleton3();
        }
        return INSTANCE;
    }
    public static void otherMethod(){
        System.out.println("otherMethod()...");
    }

}

package com.jie.singleton;

/**
 * @Author 小杰
 * @Date 2022/5/16 16:03
 * @Version 1.0
 * 懒汉式-DCL
 */
public class Singleton4 {
    //可见性、有序性。
    public static volatile Singleton4 INSTANCE=null;
    private Singleton4(){
        System.out.println("private Singleton4()...");
    }
    public static Singleton4 getInstance(){
        if (INSTANCE==null){
            synchronized (Singleton4.class){
                if (INSTANCE==null){
                    INSTANCE=new Singleton4();
                }
            }
        }
        return INSTANCE;
    }
    public static void otherMethod(){
        System.out.println("otherMethod()...");
    }
}

package com.jie.singleton;

/**
 * @Author 小杰
 * @Date 2022/5/16 16:26
 * @Version 1.0
 * 懒汉式-内部类方式
 */
public class Singleton5 {
    private Singleton5(){
        System.out.println("private Singleton5()...");
    }
    private static class Holder{
static Singleton5 INSTANCE=new Singleton5();
    }
    public static Singleton5 getInstance(){
        return Holder.INSTANCE;
    }
    public static void otherMethod(){
        System.out.println("otherMethod()...");
    }
}

参考

单例模式详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值