• Post Reply Bookmark Topic Watch Topic
  • New Topic

utility of singleton classes  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ranchers,
please tell me with real life example what is the utility of singleton classes.
Regards
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satwik sharma:
hi Ranchers,
please tell me with real life example what is the utility of singleton classes.
Regards


Although this forum is very lenient in the type of questions that may be asked -- please keep in mind that this is a certification forum. Moving this to Java in General (Beginner).

Henry
 
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The singleton pattern allows the existence of only one instance of a class at any time. Usually you have a getInstance() method that will return a reference to this instance.

You can find many arguments about the need for such a pattern when static fields will do the trick. Google for "singleton vs static" to read those arguments.

[EDIT]

I guess any thing that you would want to implement with a static field can be managed with a singleton.
[ December 25, 2006: Message edited by: John Meyers ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Singleton class asures that for these types of classes only one instance of this class will exist. These classes must be used as Utility classes
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A singleton pattern allows you to create only one instance of a class at one time. This helps you that the class wont have multiple instances created for it. The only one instance becomes the global point of access to it. Live example is a print spooler where at a time only one instance of the job is needed. For creating the same:

public class DemoSingleton {
private static DemoSingleton instance = null;
protected DemoSingleton () {
// Exists only to defeat instantiation.
}
public static DemoSingleton getInstance() {
if(instance == null) {
instance = new DemoSingleton();
}
return instance;
}
}


call it using:

public class SingletonInstantiator {
public SingletonInstantiator() {
DemoSingleton instance = DemoSingleton.getInstance();
DemoSingleton anotherInstance =
new DemoSingleton();
...
}
}

The above create only one instance if it does not exists, otherwise will return the same instance every time.
I hope that it solves
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "private constructor plus static getter" shown is the most common Singleton implementation, but not the only one. We could use class visiblity in packages or inner classes and probably other tricks, too.

The static getter as shown is not thread safe. Two threads could wind up creating two instances instead of one. You can synchronize the getter or create the instance right where you declare the static variable. Don't use any "double check" techniques you might find in old web articles. It is possible for those to fail, too.

Even when the getter is working at its best, be aware that it can only guarantee one instance per classloader. If we cause the class to be loaded by two classloaders, it will create two instances. Most days we don't think about class loaders and this is good enough, but some frameworks use multiple loaders and there can be confusing surprises.

Hope that helps!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!