File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Singleton as base class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Singleton as base class" Watch "Singleton as base class" New topic

Singleton as base class

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Hello all
how can i design my singleton class , that every class that i will like to be singleton
will inherit or implement the singleton class and will become single tone
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
hmmmm, as far as I know (and I could be wrong), you cant inherit from a class with a private construct, because the inheriting class cant call the super constructor (as its private.

If you supply a constructor with a more open modifer you could inherit from it, but the class would no longer by a singleton.

Are you sure you need to extend the singleton? perhaps a different pattern is more suited to your problem?

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
It's not clear from your question whether you want a single singleton, which is allowed to be of the base class or any subclass, or whether you want separate singletons of the base class and every subclass. I'm guessing it's the former.

If you make the base class constructor store "this" as the singleton instance, in a static field of the base class, you have some of what you may want. You can't easily do lazy instantiation of the singleton, within a getSingleton() method, which people commonly like to do.

To have any sort of hierarchy of singleton classes, they need "protected" constructors. In Java, "protected" implies package access. This reduces the singleton-ness of the classes.

Singleton is, in many opinions, an over-used and mis-used pattern. One of the objections to it is that it doesn't play very nicely in complicated class hierarchies like you're describing. Are you sure you couldn't achieve what you want in a tidier way, without lots of singleton classes?
[ April 02, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Peter Chase:

In Java, "protected" implies package access.

In java noting/default access specifier means package access and protected means that classes extending this class can see the methods / variables marked as protected.

I do not think that it will be possible to make a base class for singleton ,and just by inheriting this base class you cannot achieve singleton behaviour.

Following I think are must for singleton.

private constructor
static method that you give you the reference of the object.

How would you achieve the above ?

Rahul Bhattacharjee
LinkedIn - Blog
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Very cool question ... trying to put the mechanics of making and accessing singletons into a base class. I think the answer is still "no" I don't know how to do it.

Following I think are must for singleton.

private constructor
static method that you give you the reference of the object.

Neither of those are "musts" for Singleton, though they are far and away the most common elements in Java. Singleton is a design pattern, not a chunk of code, and there are other ways to achieve the two goals: controlling the number of instances (usually one, but maybe not) and providing access. See if you can think of a couple.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Yes , I agree with you.
I agree. Here's the link:
subject: Singleton as base class
It's not a secret anymore!