File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Real World Example of Singleton Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Real World Example of Singleton Pattern" Watch "Real World Example of Singleton Pattern" New topic

Real World Example of Singleton Pattern

Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191

Can anyone please give me a real world example of using singleton pattern.

Vinny Menon
Ranch Hand

Joined: Jan 10, 2006
Posts: 62

Hi Vineela,
one real world example would be:
in one of my earlier projects,we had used the business delegate design pattern.i.e.
we had used struts framework and within the action class ,we made call to the business delegate classand from the business delegate class we made the call to the session bean.

we didnt want multiple instances of the business delegate and wanted to control the instance of the Business Delegate.hence we made it a singleton

the flow is like this..

Jsp-->action class-->Business Delegate(Singleton)-->Session Beans-->entity bean-->Database.

vinny m

Regards,Vinny M.
proud Fan of European Champion CHELSEA FC
"If you don't see the bug where you're looking, perhaps you're looking in the wrong place" -James Gosling
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
Many services in a J2EE server are arguably instances of the singleton pattern. At any one time only one variation of a specific service will be in use by the server, but different configurations of the server will allow different choices of service. For example, there will only be one global JNDI tree to search (one in the sense that there is a unique root starting point), but the implementation of that JNDI could be something vendor-specific, an external LDAP, Active Directory, etc.

Reid - SCJP2 (April 2002)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I've used them frequently for caches, factories and statistics utilities like a hit counter.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ranch Hand

Joined: Aug 18, 2005
Posts: 372
Thread pools, connection pools, registry objects, objects handling user preferences, caches
I agree. Here's the link:
subject: Real World Example of Singleton Pattern
It's not a secret anymore!