其他分享
首页 > 其他分享> > 枚举实现单例模式

枚举实现单例模式

作者:互联网

枚举是单例实现中唯一一种不会被破坏的单例实现模式

public class SingletonObject {

    private SingletonObject() {
    }

    private enum Singleton {
        INSTANCE;
        private final SingletonObject instance;

        Singleton() {
            instance = new SingletonObject();
        }

        private SingletonObject getInstance() {
            return instance;
        }
    }

    public static SingletonObject getInstance() {
        return Singleton.INSTANCE.getInstance();
    }
}

标签:Singleton,getInstance,private,instance,枚举,模式,单例,SingletonObject
来源: https://www.cnblogs.com/zhu12/p/16375476.html