Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton as base class

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

G
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , I agree with you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic