Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

singleton and inheritance in Java

I have a base class that captures some functionality common to two classes. In other words, I can create one base class and make these two classes subclasses of that base class. However, for each of these sub classes, the number of instances can be created is 1 (i.e. each sub class has to be a singleton). I googled and found that there's a reasonable debate going on over this. Although there are several solutions available, I am not sure whether they would fit in my case.

can anyone tell me how I should design this?

like image 604
sura Avatar asked Mar 25 '11 02:03

sura


People also ask

Can a singleton class be inherited in Java?

Unlike static classes, Singleton classes can be inherited, can have base class, can be serialized and can implement interfaces.

What is Singleton give example?

In Java, Singleton is a design pattern that ensures that a class can only have one object. To create a singleton class, a class must implement the following properties: Create a private constructor of the class to restrict object creation outside of the class.

Can we override singleton class?

The only way to override a singleton is to have a singleton that expects to be overridden. The simplest way to do this is to provide Singleton that implements an interface (or is otherwise fully abstract itself) that internally instantiates an injected singleton upon the first use of getInstance() .

What is Singleton used for?

Singleton classes are used for logging, driver objects, caching and thread pool, database connections. An implementation of singleton class should have following properties: It should have only one instance : This is done by providing an instance of the class from within the class.


2 Answers

Use the Abstract factory pattern. Have a separate class with methods to retrieve the singletons, and let it hold the references to the singletons in instance variables or in a map.

You may not want the increased complexity, but frameworks like Spring were created to solve these kind of issues (among others).

It seems that Pico Container is alive and well, and it may be the simplest while still solid solution. Look at the inversion of control topics, and let the framework inject the singletons where you need them.

In short, don't try to make the singletons manage access to themselves. Delegate that on something else.

There's nothing inherently wrong in having singleton classes with complex inheritance. In fact, class hierarchies with private constructors (no instances) are very useful in many situations. You just have to decide how you want to manage the two important aspects of singletons: creation, and access.

like image 120
Apalala Avatar answered Oct 26 '22 22:10

Apalala


You can make each class separately a singleton, and make the base class abstract. Not sure what's the debate -- just that singletons, in general, aren't a great idea?

like image 30
Ernest Friedman-Hill Avatar answered Oct 26 '22 22:10

Ernest Friedman-Hill