Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Query for Singleton pattern

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can a singleton pattern exist without having private constructor? I got an example where constructor is public.Can somebody explain this things to me? Here is the code:-

class SingletonException extends RuntimeException
{
public SingletonException()
{
super();
}
public SingletonException(String s)
{
super(s);
}

public class PrintSpooler {
//this is a prototype for a printer-spooler class
//such that only one instance can ever exist
static boolean
instance_flag=false; //true if 1 instance
public PrintSpooler() throws SingletonException
{
if (instance_flag)
throw new SingletonException("Only one spooler allowed");
else
instance_flag = true; //set flag for 1 instance
System.out.println("spooler opened");
}
//-------------------------------------------
public void finalize()
{
instance_flag = false; //clear if destroyed
}
}
}

public class singleSpooler
{
static public void main(String argv[])
{
PrintSpooler pr1, pr2;
//open one spooler--this should always work
System.out.println("Opening one spooler");
try{
pr1 = new PrintSpooler();
}
catch (SingletonException e)
{System.out.println(e.getMessage());}
//try to open another spooler --should fail
System.out.println("Opening two spoolers");
try{
pr2 = new PrintSpooler();
}
catch (SingletonException e)
{System.out.println(e.getMessage());}
}
}
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sahid,

Though keeping the constructor private is the traditional and famous way which everyone does, the code you had pasted also seems to achieve the same but having the constructor public.

The same logic of checking the instance (here, its a boolean flag) for 'NOT NULL' before actually instantiating the class and returning it is present inside the public constructor here whereas it would be in the private constructor.

I think it seems to achieve the same functionality behind the scenes.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But here, they throw an error if there is an instance already existing which would not be the case in the traditional private constructor approach.

Anyways, its optional and left to the developer i would say. However you want to make, you can. But the purpose should be saved!
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I dont think this works, and I bet it's not thread safe.

Singletons are tricky. You should know why you want a singleton and how it is used (per JVM, per cluster, per ???). Is this to manage some globale resource, or to prevent unneccessary objects with no internal state?

Those discussions beside, look at the discussions in the following links for a better understanding of the issues.

Ramen

JGuru
Sun Java forum
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fisrt of all its not a singleton behaviou.In case there exists an object then if should return the reference to that , not an exception.I guess it has been coaded with some specific desing requirement in mind.

One more thing as what said by Ramen Chatterjee , it all depends on the application design.It might by strictly one per JVM , one per classloader ect.In fact in many cases binding resource to JNDI is not preferable rather creating a singleton(Like in case of a application deployed in clustered environment.)
 
Ranch Hand
Posts: 376
Scala Monad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also another issue with having a public constructor in the singleton:
If somebody reads the code x=new PrintSpooler(), probably will think is getting a *new* instance of PrintSpooler and will get confused.
If you have x=PrintSpooler.getInstance(), is a familiar singleton call or at least it will let him know that something is different.
That makes a whole difference for maintainability.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do some other tricks with visibility of the Singleton class so it has a public constructor but the class can't be seen outside a containing class or package.

The example at the top misses the second half of the GoF definition of Singleton which is to provide a way to control instance creation and to provide a well known point of access to the single instance.

And we forgot to welcome Sahid to the ranch on the first post! Welcome aboard!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Stan James:

The example at the top misses the second half of the GoF definition of Singleton which is to provide a way to control instance creation and to provide a well known point of access to the single instance.



Yes, it's more like an enforced Just Create One...
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramen - The example up above is definitely not thread-safe. There's no synchronization on the setting of instance_flag.

J
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!