wood burning stoves*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Singleton Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Singleton Pattern" Watch "Singleton Pattern" New topic
Author

Singleton Pattern

Sam Stackly
Ranch Hand

Joined: May 04, 2002
Posts: 109
Hi guys,
a quick question , do we need to use singleton pattern in client side? (both local and server db)?
Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Singleton where? Actually I didn't use Singleton pattern in any of my code. But you can if you want.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sam Stackly
Ranch Hand

Joined: May 04, 2002
Posts: 109
Well like if we can have multiple client in one machine?

Sam
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
I am personally going to use the singleton pattern for my factory on the rmi side.
Hope that helps.
-Matt


-Matt<br /> SCJP2<br /> SCJD
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Ah, Singleton, my poor Singleton, of all patterns the GoF identified you must feel the most battered and bruised -- used more often than any other in all manner of places where you don't fit.
Singleton is appropriate wherever there is a fundamental reason why you cannot or should not have more than one copy of an object.
Is there any fundamental reason why you cannot or should not have more than one Data? Well, to the contrary, I'd say.
Steer well clear.
- Peter
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Peter that was very poetic. You should write more books.
I had tears in my eyes, thinking about that poor design pattern called Singleton.
Mark
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
what about the DataAccessFactroy. would that not be a candidate for a Singleton ? that one factroy object can handle any pooling of DataAccess objects ?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Mark Spritzler wrote:

Peter that was very poetic. You should write more books.

The muse suddenly came to me, and I thought I could extend Peter's poetry a little
(inspired by Shakespeare, Peter, and the article "When is a Singleton Not a Singleton?" at
http://developer.java.sun.com/developer/technicalArticles/Programming/singletons/#resources):
The Singleton, -- to use or not to use?
The grace of pattern overwhelms me, --
You've been the subject of abuse,
Or so the ranch sheriffs try to tell me
The heat of virtual hot spot machine
Make all my initializers lazy
But class loaders are really mean, --
They make two instances, amazing!
And then the garbage man comes in
To claim the memory unreferenced
And when I call it from the bean
It's doubleton, as if I had no preferance!
The call to get an instance comes along,
But quickly gets preemted by someone to blame.
I now have a tripleton, and I am prone
To crashes, errors, tears, and paine.
Eugene Kononov.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

what about the DataAccessFactroy. would that not be a candidate for a Singleton ? that one factroy object can handle any pooling of DataAccess objects ?

Actually what would be the problem of having a thousand DataAccessFactory(s) in the client, I could take any one of them call the "Make" method to make by Object, and be sure that I could use it without any worries.
Now of course in my coding I am not going to create more than one instance of the DataAccessFactory, but that is me coding, I don't need the Singleton Pattern to save me in any way shape or form in this case
Mark
Oh, and Eugene you're right on.
"The Quality of Singleton Pattern is not strain.."
Jason Voegele
Greenhorn

Joined: Oct 12, 2001
Posts: 9
Originally posted by Peter den Haan:

Singleton is appropriate wherever there is a fundamental reason why you cannot or should not have more than one copy of an object.
Is there any fundamental reason why you cannot or should not have more than one Data? Well, to the contrary, I'd say.

In my implementation I'm using two static factory methods in Data.java: one called open, the other called create. These methods use a HashMap that contains previously created instances of class Data, keyed by a File object representing the file containing the database. This scheme ensures that only one instance of class Data exists per file within a single JVM. This is of course a variant of the Singleton pattern, and one which I think applies to Data.
What do you think?
-- Jason Voegele
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton Pattern
 
Similar Threads
running only one instance of java program
Hibernate SessionFactory is initialized/read with every EJB call
Limited no of Objects
how to make single instance of a class?
Static methods Vs "Instance Methods"