Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • what to do if I need unit tests and having more than one instance would break the application? I guess I'll have to design UserInterface some other way in order to prevent it from doing any "damage" if instantiated twice or so Commented May 14, 2021 at 12:00
  • @Wiktor: to be fair, a component which requires the single instance can be designed to be unit-testable even when the single-instance is a singleton. The component should just not make use of the Singleton mechanics to get the instance, but injected from outside. But when all users of the singleton get the instance injected, the classic Singleton mechanics becomes pretty useless. Commented May 14, 2021 at 12:45
  • it's worth noticing that singleton doesn't have to be a class/interface with public static access. It could also be designed as a class with a static boolean hasBeenCreated check in the constructor (if true it could throw an exception) Commented May 14, 2021 at 14:35
  • @Wiktor: if a component uses x=MySingleton::getInstance() or x=new MySingleton() inside is not a huge difference in regards to unit testing that component (both variants make unit testing harder). If the component expects a single instance of UserInterface through its constructor, where UserInterface is actually an interface to the expected UserInterfaceImplementation object, then it will be easier to exchange the UserInterface by a mock for testing purposes. Commented May 14, 2021 at 15:18