0

I have very quick question. I'm discussing with my friend about Singleton pattern. They say it's not good to use in all classes, and I have a different opinion. Is this true and why it is important not to use it in all classes? It makes no sense to me. Pattern is used to prevent multiple instantiation and that is it. What's the problem?

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • If you really need to have exactly the same instance the singleton pattern makes sense. But I cannot imagine any use case where I would only need one instance (and exactly one) of every class in a project. – Markus Safar Nov 14 '18 at 20:45
  • Ok, but I used prototypes in the JS and I made a pattern for the Singleton pattern. Does the program slow down when not using the Singleton pattern in each class? – Miloš Pantelinac Nov 14 '18 at 20:54
  • My pattern https://github.com/TesalVector/Inheritance_JavaScript – Miloš Pantelinac Nov 14 '18 at 20:55
  • Hi, I too don't see any problem not doing it if it really serves my purpose, but as stated by @MarkusSafar it is rare to find a project where all classes will follow the singleton pattern without exception. – Ermac Nov 14 '18 at 20:56

1 Answers1

1

The main "problem" with a naive singleton is that it uses a static accessor. This forces you to refer to the singleton by class name when using properties or methods on it. This is called coding to the implementation (the opposite of coding to the interface). It negates one of the most useful indirection points for your architecture as it grows: if you use the singleton in many places around the application, you end up scattering the class name widely. If you had declared an interface and used that instead, and wanted to change the class name at a later date, then you would only have to modify the single line of code where the implementation is instantiated.

Coding to the interface becomes especially important when it comes to testing, where one frequently needs to swap a production implementation for a testing implementation so that you can isolate client class behaviour.

You can still use singleton. But you should hide it behind an interface so that none of the clients need to know that it is a singleton. Use dependency injection for this.

Julian Suggate
  • 627
  • 1
  • 6
  • 14
  • Uaau! Thank you very much for a good answer. I did not quite think so. I made a scheme and I use it when I work with prototypes. But Singleton pattern is always included. Is this so much the problem that the employer will make a mistake if he knows you are a junior developer? – Miloš Pantelinac Nov 14 '18 at 21:12
  • I'm not 100% sure what you're asking. But whether singleton is thought "bad" by employers depends on the company and the way they use OO and unit testing (if at all). Some companies are more interested in writing code quickly, and deal with architecture only on a "rainy day". Those kind of projects often have a nest of singletons in their center. – Julian Suggate Nov 14 '18 at 22:05