Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Pattern

 
Sam Stackly
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singleton where? Actually I didn't use Singleton pattern in any of my code. But you can if you want.
Mark
 
Sam Stackly
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well like if we can have multiple client in one machine?

Sam
 
Matt Ghiold
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am personally going to use the singleton pattern for my factory on the rmi side.
Hope that helps.
-Matt
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic