Java单例模式

懒汉模式写法

/**
 * @author 梅花zero
 * 懒汉单例模式
 */
public class LazyMan {


    private volatile static LazyMan lazyMan;

    private LazyMan() {
        System.out.println(Thread.currentThread().getName() + "线程名称");
    }

    public static LazyMan getInstance() {
        if (null == lazyMan) {

            synchronized (LazyMan.class) {
                if (null == lazyMan) {
                    lazyMan = new LazyMan();
                }
            }
        }

        return lazyMan;
    }
	//多线程测试
    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {

            new Thread(() -> {
                LazyMan instance = LazyMan.getInstance();
            }).start();

        }

    }

}

饿汉单例模式


/**
 * @author 梅花zero
 * 饿汉模式
 */
public class HungryMan {

    private static HungryMan hungryMan = new HungryMan();

    private HungryMan (){
    }


    public static HungryMan getInstance(){

        return hungryMan;
    }


    /**
     * 反射攻击例子破坏单例
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        HungryMan instance1 = HungryMan.getInstance();
        Constructor<HungryMan> declaredConstructor = HungryMan.class.getDeclaredConstructor(null);

        declaredConstructor.setAccessible(true);

        HungryMan instance2 = declaredConstructor.newInstance();

        System.out.println("instance1 = " + instance1);
        System.out.println("instance2 = " + instance2);
    }

}

静态内部类

/**
 * @author 梅花zero
 * 静态内部类单例
 */
public class InnerSingle {

    private InnerSingle (){

        if (InnerHolder.innerSingle != null){
            throw new RuntimeException("静态内部类不允许反射!!!");
        }
    }


    public static class InnerHolder{
        private static InnerSingle innerSingle = new InnerSingle();

    }

    public static InnerSingle getInstance(){
       return InnerHolder.innerSingle;
    }


    public static void main(String[] args) throws Exception{
        InnerSingle instance1 = InnerSingle.getInstance();

        Constructor<InnerSingle> declaredConstructor = InnerSingle.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true);
        InnerSingle instance2 = declaredConstructor.newInstance();

        System.out.println("instance1 = " + instance1);
        System.out.println("instance2 = " + instance2);

    }
}

枚举单例


/**
 * @author 梅花zero
 * 枚举类天然单例防止反射
 */

public enum SingleEnum {

    //测试
    INSTANCE;

    public SingleEnum getInstance(){
        return INSTANCE;
    }

    public static void main(String[] args) throws Exception {

        //构造器的参数类型是 null 会抛出 .NoSuchMethodException: com.company.single.SingleEnum.<init> 异常,代表参数错误
        //构造器的参数类型是String.class,int.class 才会抛出 Cannot reflectively create enum objects 异常
        Constructor<SingleEnum> declaredConstructor = SingleEnum.class.getDeclaredConstructor(String.class,int.class);
        declaredConstructor.setAccessible(true);
        SingleEnum singleEnum = declaredConstructor.newInstance();
        System.out.println(singleEnum);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值