Two Laptop Bag
The moose likes EJB and other Java EE Technologies and the fly likes singletondesign pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "singletondesign pattern" Watch "singletondesign pattern" New topic

singletondesign pattern

raghuram bikkani

Joined: Jan 18, 2006
Posts: 16
are u ever used singletondesign pattern in ur project tell me what is the situation?
Daniel Rhoades
Ranch Hand

Joined: Jun 30, 2004
Posts: 186
The most common use is in web tier ServiceLocators.

Drinking more tea is the key...
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
Examples include looking up EJB Home, getting Logger.

The Runtime class in Java is also a singleton.

Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
The "trick" with singleton is to not over-use it. A common mistake in application design comes from thinking "gee, I don't think I'll ever bother creating more than one of these, so I guess I should use singleton". The whole point of singleton isn't just that you only see yourself using one instance. It is that fundamentally your app would be broken if you tried to use more than one instance, and that the implementation of that instance could vary (e.g. by subclassing or creating multiple implementations of an interface).

Objects representing external resources like your operating system services (java.lang.Runtime) or windowing environment, or sometimes an object creation factory tend to make for good examples of using singleton effectively. Karthi's example of Logger is a good example, particularly if you think in terms of the Jakarta commons logging API instead of Log4J. It isn't the Logger instance itself which is the singleton, it is really the logging implementation behind it that is the true singleton. The implementation could be Log4J or Sun's logging API or something else. By changing the configuration of commons logging, all your code would get a new logging environment without any code change.
[ January 19, 2006: Message edited by: Reid M. Pinchback ]

Reid - SCJP2 (April 2002)
I agree. Here's the link:
subject: singletondesign pattern
It's not a secret anymore!