Teddy
Teddy

Reputation: 4223

Java singleton usage

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?

Upvotes: 1

Views: 2694

Answers (3)

davidxxx
davidxxx

Reputation: 131326

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.

Upvotes: 1

Matthew Franglen
Matthew Franglen

Reputation: 4532

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.

Upvotes: 2

rachana
rachana

Reputation: 3414

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

Upvotes: 3

Related Questions