枚举单例的实现

枚举单例的实现

单例是什么我在这里不再详细说了,其他的饿汉模式单例、懒汉模式单例、双重加锁这么的在此都不讨论。我参考了网上其他人写的枚举单例,都差不多,没什么实用的参考价值,这里只给出我自己实现的枚举的单例写法,并且能够防止反射,希望大家一起讨论。

枚举单例

package com.example.demo.enums;

import com.example.demo.service.SingletonService;

/**
 * @classname: Singleton
 * @description: 单例示例
 * @author chenhx
 * @date 2020-03-18 10:05:17
 */
public enum SingletonOfEnum implements SingletonService {
    INSTANCE{
        @Override
        public String doSomething() {
            return "aaa";
        }

        @Override
        public void doPrint(String str) {
            System.out.println("this is a function doPrint:" + str);
        }
    };

    public static SingletonOfEnum getInstance(){
        return INSTANCE;
    }
}

其中枚举实现了接口SingletonService,这个接口里可以定义一些我们需要做的一些事,例如

SingletonService.java

/**
 * @classname: SingletonService
 * @description: 单例接口
 * @author chenhx
 * @date 2020-03-18 14:18:54
 */
public interface SingletonService {
    String doSomething();
    void doPrint(String str);
}

测试

    @Test
    public void test3() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        //通过枚举获取单例
        SingletonOfEnum instance = SingletonOfEnum.getInstance();
        String s = instance.doSomething();
        System.out.println(s);
        instance.doPrint(s);
        System.out.println(instance.hashCode());

        //通过枚举获取单例
        SingletonOfEnum instance2 = SingletonOfEnum.getInstance();
        System.out.println(instance2.hashCode());

        // 通过反射获取单例
        Constructor<SingletonOfEnum> constructor = SingletonOfEnum.class.getDeclaredConstructor();
        constructor.setAccessible(true);
        SingletonOfEnum singleton3 = constructor.newInstance();
        System.out.println(singleton3);
    }

通过观察控制台,我们发现,通过枚举获取的单例对象确实是同一个,但是通过反射获取实例,就会出现异常,因为枚举是不能通过反射去调用他的私有构造方法的。
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章