Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Question

 
Hrishikesh Ghatnekar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to create one singleton class and planning to use it in my application.
My question is what will be difference if I try to access the methods of the singletone class in following ways

1)Singletone.getInstance().methodName()
2)Singletone.methodName()

In Second case how come the program work without going thorough the constructor?


The code for my program can be

public class Singltone_impl {

private static Singltone_impl validator = null;

protected Singltone_impl() {
System.out.println("In Singltone_impl constructor <=");
init();
System.out.println("Singltone_impl constructor <=");
}

protected void init() {
System.out.println("In Singltone_impl init <=");
System.out.println("In Singltone_impl init =>");
}

public static Singltone_impl getInstance() {
synchronized (Singltone_impl.class) {
if (validator != null) {
return validator;
} else {
validator = new Singltone_impl();
return validator;
}
}
}

public static boolean validate(String email_id) {
System.out.println("In Singltone_impl validate ");
if (email_id == null || email_id.trim().length() < 1) {
System.out.println("Singltone_impl , email ID not present , returning false ");
return false;
}
else {
System.out.println("TRRRRRRRRRRUEEEEEEEEE");
return true;
}
}



}

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "getInstance()" method of a Singleton class is supposed to be static, as it's used to get a reference to the singleton instance. But all the other methods are supposed to be instance (i.e., non-static) methods -- otherwise, the single instance is itself useless, and instead you've just got a class with all static methods.
 
Hrishikesh Ghatnekar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Very Much correct.

Thanks for the help


:-)
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


=> this will only work with static methods of your class (a not very OO approach)



=> this makes sure your Singletone instance has been constructed and that initialization has occured before you call methodName(). You have more control with this approach.
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Hrishikesh ,hope this helps you

class Singleton
{
private static Singleton instance;

private Singleton()
{
}

public static Singleton getInstance()
{
if (instance == null)
{
instance = new Singleton();
return instance;
}

return instance;
}

}

Cheers



I have always depended on the kindness of strangers....
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by faisal usmani:
hello Hrishikesh ,hope this helps you


Not so much; you took his thread-safe version and made it not thread-safe; not clear to me why you offered this.
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOps forgot to make it thread safe !!!

class Singleton
{
private static Singleton instance;

private Singleton()
{
}

public static synchronized Singleton getInstance()
{
if (instance == null)
{
instance = new Singleton();
return instance;
}

return instance;
}

}


Does this makes sense with respect to thread safety :roll:

regards
[ April 18, 2006: Message edited by: faisal usmani ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic