Examples of singleton classes in the Java APIs

前端 未结 3 1808
闹比i
闹比i 2021-02-04 12:23

What are the best examples of the Singleton design pattern in the Java APIs? Is the Runtime class a singleton?

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 13:17

    Note singletons should be used with care and reflection. Consider the arguments against singletons and your situation before you implement one. Overuse of singletons is an anti-pattern - similar to global variables.

    Singleton Wiki Article

    Java Dev on Singletons

    Why Singletons are Evil

    I've used them in the past and see some benefit to them. I've also been highly annoyed when trying to do test driven development with them around that's one area where they are evil. Also inheriting from them results in some hard to understand behaviour - at least in Python - I don't know for sure in Java. Generally you just don't do it because of that. So like threading these seem like a great idea at first and then you run into the pitfalls and realize well maybe this isn't so good after all.

提交回复
热议问题