Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending a Singleton Class

 
sree harsha.hn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can somebody explain the concept of extending a Singleton Class?

Thanks,
Harsha
 
vicky Aggarwal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ideally singleton class have private constructor.
We can override a class having private constructor beocz super constructor will not be accessible.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually you wouldn't extend a Singleton class. In fact, usually a Singleton class would be designed in a way to prevent it being extended, in case extending it was used as a way to create multiple instances.
 
bernard amadeus
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Usually you wouldn't extend a Singleton class. In fact, usually a Singleton class would be designed in a way to prevent it being extended, in case extending it was used as a way to create multiple instances.

I think that the definition of a Singleton as having only one instance is misleading: better say that only one combination of values (object "state") is involved in methods implementation.
So curiously you can create a Singleton that is implemented without the calling code being aware of this property: the different instances created by client code all delegate to a single object!
and yes you can create subclasses of this façade.
 
sree harsha.hn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Thanks for your responses.

I still didnt get a concrete answer about whether we can Extend a Singleton Class or not.

Could you please post the code snippets?

Thanks,
Harsha
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can attempt to do it the exact same way you'd extend any other class...however, depending on how the singleton is written it may not actually work. It is impossible to say without knowing more about the specific singleton implementation you are trying to extend, which clearly we don't have. But all you'd do is

 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you need to extend a singleton class is a constructor with protected or package-default in the singleton class. If there are only private constructors you simply won't be able to extend it. If there are public constructors then it's not a singleton class.

Note that to use a package-default constructor your class needs to be in the same package as the singleton class; visibility of constructors is no different than the visibility of methods or fields.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, you should never actually do this. Singletons are evil and should be avoided.
 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Harsha-

Just for sake of my own interest, may I ask you why are looking to extend Singlton class Or where you got this requiremtn from ?

What i understand is Singlton is just a concept that one instance per JVM (or per App in case of App servers like WLS).
you can implement this using different ways,
the most common way I have seen is making Contructior private and providing Factory method to control Instantiation: in this case definitely Extends would never work since constructor is private.

I would be happy to see the Singlton implimentation you have in your mind which allows the Extension (I mean Extends), may be i would learn something more

thanks!
 
K Abhijit
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:Singletons are evil and should be avoided.

can you please elaborate on this please? Since actually I found them quite handy sometimes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic