• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton using public constructors?  RSS feed

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I wanted to implement a class as a Singleton, But I don't want to do it the classical way as shown below,
/***
public class MySingleton{
private static MySingleton theInstance=null;
private MySingleton(...){ }
public static synchronized MySingleton getInstance(){
if (theInstance==null){
theInstance=new MySingleton(...)
}
return theInstance;
}
}
******/

I want the constructor to be public, as this is a requirement.
Could anybody let me know how to implement Singleton with public constructors

Thanks in Advance
John
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class has a public constructor, then by definition, anybody can create an instance of it at any time, so it can't be a singleton, of course. The best thing I can tell you -- and it's not a very good solution -- would be to have the constructor first check a count of already created objects, and if it's greater than 0, throw an exception to abort creation of any more.
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you can tell us why your Singleton must have a public constructor? (No real need of course, I'm just curious).
 
John Vergis
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
I am making use of Spring AOP, which requires my class's constructor to be public, so that it can be proxied. Currently this particular class of mine is a Singleton, Though the Bean Factory can proxy it as an shared instance, yet if any other class would like to create an instance by not using the Bean Factory, then it is possible to do so. I wanted to avoid that.
Thanks
John
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh... Why not create a wrapper around your singleton of which you can have multiple instances each of which simply calls the one singleton?

That way you need not change your existing class and still can use it with Spring proxies.
 
John Vergis
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Sounds good.
 
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to agree, quite horrid. A wrapper class would work. Springifying the lot and always using Spring to hand out collaborators would be preferable - then you can use Spring to manage lifecycle.

- Peter
[ June 02, 2004: Message edited by: Peter den Haan ]
 
John Vergis
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter, Looks like i will have to use the code u suggested else I will end up doing a lot of refactoring, as this class is used by quite a lot of other packages.

Thanks
John
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can just discipline yourself to only create one instance. Singleton is a usage pattern, not necessarily an identifiable feature of the class. Agreed, it would be ideal if the code ENFORCED the singleton usage, but maybe not necessary.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!