Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminating Singletons

I've been doing a lot of reading about how using singleton classes is becoming bad practice in programming due to hidden dependencies, hard to test etc etc.

A lot of forum posts I have read have said that you can still maintain a singleton's main functionality of only allowing one instance without using the singleton pattern.

I wondered if anyone could give a practical example of this. A lot of posts have suggested using a factory class to create singleton instances where the dependencies are clearly shown. To me this just seems like taking multiple singletons and combining them into a single factory singleton which would have the same problems?

like image 856
BigCroyd Avatar asked Feb 06 '12 15:02

BigCroyd


People also ask

How can we overcome Singleton?

Prevent Singleton Pattern From Cloning To overcome the above issue, we need to implement/override the clone() method and throw an exception CloneNotSupportedException from the clone method. If anyone tries to create a clone object of Singleton , it will throw an exception, as shown in the below code.

Why you should not use singletons?

The most important drawback of the singleton pattern is sacrificing transparency for convenience. Consider the earlier example. Over time, you lose track of the objects that access the user object and, more importantly, the objects that modify its properties.

How do you break Singleton Behaviour?

Serialization is used to convert an object of byte stream and save in a file or send over a network. Suppose you serialize an object of a singleton class. Then if you de-serialize that object it will create a new instance and hence break the singleton pattern.

Are singletons necessary?

A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.


1 Answers

The problem with the "singleton pattern" is really not with the singleton itself, but with the inflexible static factory method in the singleton class. And I believe that even in the GoF book, this was considered an example of how a singleton could be accessed, not the definitive implementation model.

A lot of posts have suggested using a factory class to create singleton instances where the dependencies are clearly shown. To me this just seems like taking multiple singletons and combining them into a single factory singleton which would have the same problems?

The difference is that this single factory then becomes the single point where you maintain dependencies.

And in fact, the generally accepted solution is to use a dependency injection framework like Spring or Guice which is basically such a single factory with a very powerful and flexible configuration mechanism which can do much more than just manage singletons.

like image 184
Michael Borgwardt Avatar answered Oct 17 '22 22:10

Michael Borgwardt