Java Singleton Design Pattern : Questions

后端 未结 11 557
渐次进展
渐次进展 2021-01-30 11:39

I had an interview recently and he asked me about Singleton Design Patterns about how are they implemented and I told him that using static variables and static methods we can i

11条回答
  •  抹茶落季
    2021-01-30 12:26

    I find it hard to believe that so many answers missed the best standard practice for singletons - using Enums - this will give you a singleton whose scope is the class loader which is good enough for most purposes.

    public enum Singleton { ONE_AND_ONLY_ONE ; ... members and other junk ... }
    

    As for singletons at higher levels - perhaps I am being silly - but my inclination would be to distribute the JVM itself (and restrict the class loaders). Then the enum would be adequate to the job .

提交回复
热议问题