That's not a Singleton - that's a bunch of global functions. Global functions have their legitimate place, but that's few and far between.Originally posted by aakash bhatt:
Why do we need to make a class singleton when we can achieve the same by declaring attributes and methods as static.
That's the classic simple-minded Java Singleton pattern. In fact, it's a horrible way to implement it; as you note, it's difficult to get much mileage out of inheritance; there's no good opportunity for pluggability, testing is difficult where the Singleton is a collaborator that you want to stub, and so on. Fortunately that is by no means the only way to implement it. For example, you can make the Singleton class configurable and use Class.forName() to instantiate your Singleton in the (static) factory method. The next step could be to give responsibility for instantiating and managing the Singleton to a separate factory class. Take that idea further and you get the Abstract Factory pattern or a repository. Take that further still and you end up with something like the Spring bean factory. Which is how I create most of my Singletons these days.2) Inheritance
While i didn't still got cleared with inheritance as when i extends the inherited class the getInstance() method in base singleton class
is static, as it is static I cannot override it.
The Gang of Four list the following.Apart from the above 2 point are there any more advantages
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
santhana Marimuthu wrote:1.Singleton represent object and Static class represent a method.
2. Static class provides better performance than Singleton pattern, because static methods are bonded on compile time.
3. One more difference between Singleton and static is, ability to override. Since static methods in Java cannot be overridden, they leads to inflexibility. On the other hand, you can override methods defined in Singleton class by extending it.
4. Static classes are hard to mock and consequently hard to test than Singletons, which are pretty easy to mock and thus easy to test. It’s easier to write JUnit test for Singleton than static classes, because you can pass mock object whenever Singleton is expected, e.g. into constructor or as method arguments.
santhana Marimuthu wrote:1.Singleton represent object and Static class represent a method.
santhana Marimuthu wrote:2. Static class provides better performance than Singleton pattern, because static methods are bonded on compile time.
santhana Marimuthu wrote:3. One more difference between Singleton and static is, ability to override. Since static methods in Java cannot be overridden, they leads to inflexibility. On the other hand, you can override methods defined in Singleton class by extending it.
santhana Marimuthu wrote:4. Static classes are hard to mock and consequently hard to test than Singletons, which are pretty easy to mock and thus easy to test. It’s easier to write JUnit test for Singleton than static classes, because you can pass mock object whenever Singleton is expected, e.g. into constructor or as method arguments.
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|