File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to program base class as singletone that other class's when extend it will become singletone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to program base class as singletone that other class Watch "how to program base class as singletone that other class New topic
Author

how to program base class as singletone that other class's when extend it will become singletone

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Hello all
i need to create several single tones , and i like to make the class that have to be singleton just to inherit or implement some kind base class
and then it will become singletone .
how can i do that ?
Surya Kant
Ranch Hand

Joined: Mar 29, 2005
Posts: 104
I think it is not possible because for singleton class you will be declaring constructor as private which will stop you from overriding the class.
But i would like to know the answer if you find any.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Its really difficult to create a base class for singletons, since the most important method in the singleton is this one:



and since it is static you can't define it as abstract and force the user to define it. so the instance() method could not be put in an abstract class nor in an interface.

Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10270
    
    8

You can always call a public abstract method from inside your private constructor to do the custom initialization stuff. Something like


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
You can always call a public abstract method from inside your private constructor to do the custom initialization stuff. Something like

private MySingleton(){
//Do base/common intializing stuff here
customInitialize(); //this is an abstract method in your base abstract class
}


and how can you call this method from the outside ? you never initialize a singleton using its constructor, you have to call the static getInstance() method on its class.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Maneesh Godbole wrote:You can always call a public abstract method from inside your private constructor to do the custom initialization stuff.

But if you only have a private constructor, you cannot override the class.

If a singleton class needs to be overridden, you will need to make the constructor protected, or at least not private (default access may be just fine). However, that does allow sub classes to call the constructor, and there is nothing preventing sub classes from making their own constructors public, effectively making it a non-singleton class.

The only way I can think of to enforce sub classes to be singleton classes as well is to keep track of whether or not a class is already instantiated. The constructor of the base can then keep that list up to date:
(synchronization omitted for simplicity)
It doesn't enforce classes to be a true singleton though - it's still possible to create one single instance through a public constructor. After that there will be no more instances though.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10270
    
    8

Rob Prime wrote:
Maneesh Godbole wrote:You can always call a public abstract method from inside your private constructor to do the custom initialization stuff.

But if you only have a private constructor, you cannot override the class.


Yup. You are absolutely right. I missed the "private" and stand corrected.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to program base class as singletone that other class's when extend it will become singletone