1

Specifically in Java, what are the uses of singletons? Seems like whatever can be done with a singleton can also be done just with static methods and variables. Is there something I'm missing. Many users suggest lazy loading as a reason however it looks like that's not really a benefit in Java.

The only benefit I can think of is that the implementation could be enhanced though a overridden implementation in a new class, which is substituted in the getInstance() method.

Update/edit:

Advantage of Singleton over static is that former is more object oriented than later. With Singleton, you can use Inheritance and Polymorphism to extend a base class, implement an interface and provide different implementations.

(Looks like this is a way to achieve results similar to overriding of static methods in Java.)

Are there any other advantages aswell?

Teddy
  • 4,009
  • 2
  • 33
  • 55
  • 1
    `"The only benefit I can think of is that the implementation could be enhanced though a overridden implementation in a new class, which is substituted in the getInstance() method."` That would be a faulty implementation of the Singleton pattern in Java because you'd be free to instantiate more than one object of the supposed singleton by creating a subclass and allowing the creation of its instances by means other than the `getInstance` method. – toniedzwiedz Jun 22 '14 at 12:09
  • I didnt mean to circumvent the singleton using subclassing. I meant only one instance of the subclass through the getInstance itself.. for example through a package-visible class. – Teddy Jun 22 '14 at 13:04
  • Possible duplicate: http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern – kajacx Jun 22 '14 at 13:27
  • @kajacx Although many of the points are applicable that question isn't language-specific. I want to be more clear on the java-specific impact. Some languages seem to allow static method overriding and in some languages lazy loading seems to be an advantage of singleton method. Also, garbage collection impact is of no concern in Java. – Teddy Jun 22 '14 at 13:33

3 Answers3

3

The Singleton's purpose is to control object creation, limiting the number of obejcts to one only. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources such as database connections or sockets.

For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.

The Singleton design pattern addresses all of these concerns. With the Singleton design pattern you can:

  • Ensure that only one instance of a class is created
  • Provide a global point of access to the object
  • Allow multiple instances in the future without affecting a singleton class's clients

for more information read this and also see

Difference between Singleton Pattern vs Static Class in Java

rachana
  • 3,344
  • 7
  • 30
  • 49
  • In the article you have linked, this is the only advantage of Singleton mentioned(which looks close to what I was thinking of, with some more clarity): Main advantage of Singleton over static is that former is more object oriented than later. With Singleton, you can use Inheritance and Polymorphism to extend a base class, implement an interface and provide different implementations. – Teddy Jun 22 '14 at 13:08
2

The wikipedia page on the pattern indicates several common uses:

Facade objects are often singletons because only one Facade object is required.

State objects are often singletons (i.e. a global state).

Singletons are often preferred to global variables because:

  • They do not pollute the global namespace (or, in languages with namespaces, their containing namespace) with unnecessary variables.
  • They permit lazy allocation and initialization, whereas global variables in many languages will always consume resources.

Effective Java recommends that you create a singleton by using a single value enum to prevent multiple instances being created. This also prevents any subclassing. This does have the side effect of making testing harder.

One of the benefits of using a singleton is that it becomes much easier to control concurrent access, as the singleton object itself is ideal as the locking object for synchronisation.

Matthew Franglen
  • 4,441
  • 22
  • 32
  • I understand the frequent use of single instance objects. Just that in many cases the same functionality can be achieved using just static methods. The reason for choosing between static class and singleton is more refined.. For example, Why facade cant be implemented as using static methods.. – Teddy Jun 22 '14 at 13:38
  • You may wish to test classes that use the facade in which case using an object makes it much easier, as you can substitute a mock object. This is especially easy if the singleton implements an interface. – Matthew Franglen Jun 22 '14 at 13:39
1

Advantage of Singleton over static is that former is more object oriented than later. With Singleton, you can use Inheritance and Polymorphism to extend a base class, implement an interface and provide different implementation

I agree with it.

Are there any other advantages aswell?

Using it as an object . You cannot pass a static class as an object in a parameter for example.

davidxxx
  • 125,838
  • 23
  • 214
  • 215