aspose file tools*
The moose likes Cattle Drive and the fly likes patterns on the cattle drive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "patterns on the cattle drive" Watch "patterns on the cattle drive" New topic
Author

patterns on the cattle drive

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6

In the jdbc-2 instructions, we're told to use the soup database instead of the servlet singleton. I've overheard conversations about the singleton pattern, but haven't done what it takes (read: I'm lazy!) to get a real understanding of it. So along comes this reference to it with code I'm familiar with. A chance not to be missed...
...so, continuing in my lazy vein, I'll ask: anyone into explaining what makes the servlet we wrote a singleton?

And on a related note, I've recently considered buying "Design Patterns Explained". Opinions?
Sure feels good to be an active driver again.
John Hembree
hired gun
Ranch Hand

Joined: Mar 07, 2003
Posts: 250

It has no public constructors, you end up calling a static getInstance type of method, which then checks to see if it is initialized, if not it initializes the servlet and then hands back a reference to it. If you do a search on the web you can find some good references on it and they will explain it from both sides of the fence.
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
> And on a related note, I've recently considered buying "Design Patterns Explained". Opinions?
i have not read that book yet, but i do have another suggestoin - we used the "Gang of Four" book (aka "Design Patterns") in my Advanced OO Design Patterns class last semester... great book!
Sure feels good to be an active driver again.
we'll see how i feel after my first nitpick.


what?
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
i have not read that book yet, but i do have another suggestoin - we used the "Gang of Four" book (aka "Design Patterns") in my Advanced OO Design Patterns class last semester... great book!
Yeah, I was thinking of a lighter intro than the famous GOF tome, though. That *was* for your advanced course after all.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
calling a static getInstance type of method, which then checks to see if it is initialized, if not it initializes the servlet and then hands back a reference to it.
And if it is? Does it hand back a reference to the previously initialized servlet? Is that how it ensures that there's only one instance?
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
PM: In the jdbc-2 instructions, we're told to use the soup database instead of the servlet singleton.
Unless I'm completely misremembering...
That statement is a total red herring. The instructor's solutions for the later servlets assignments *don't* use a servelet singleton at all (or at least perhaps what I understand a servlet singleton to be). Some time a year or so ago I asked a cryptic question along the lines of "what's the deal with the JDBC instructions hinting that the servlets assignments use a singleton, but the instructor's solutions don't?" but I don't think there was much discussion of that point. (Some of this might have also been cast as surprise on my side at the instructor's approach.) For the sake of crypticism and not spoiling the assignments, of course I won't say what the instructor's solutions *do* use.
I did some of the servlets assignments with a singleton (and passed). But at some point (can't remember whether it was Servlets 4b or Servlets 5) I switched over to the instructor's approach because it handles certain things much more cleanly than the singleton approach seemed to for me.
If you're interested in seeing my (but passing) versions of whichever ones I did with a singleton, let me know and I'll dig them out.
I've overheard conversations about the singleton pattern, but haven't done what it takes (read: I'm lazy!) to get a real understanding of it. So along comes this reference to it with code I'm familiar with. A chance not to be missed...
Again, as far as I can tell, the instructor's solutions don't use a singleton.
The OO and patterns forum here on JavaRanch has a *lot* of discussion of why and when you *shouldn't* use singletons -- you might want to include that in any reading you do on the topic. Also be aware that singleton implementations in Java that use lazy intitialization are not threadsafe. (DPE will explain a double-locking idiom to get around that -- but I'm a bit fuzzy on that.)
And on a related note, I've recently considered buying "Design Patterns Explained". Opinions?
DPE is good gentle introduction to patterns. I highly recommend it. It's light on code (more code sketches than full working examples), but really gets you thinking about 00. Patterns aside, I found some of their dicussion of basic OO concepts to be the most enlightening I've read. One of the authors' points about the chief difference between functional decompostion/procedural programming and object orientation being the *shift of responsibility* from calling code to called code (somewhat along the lines of "tell, don't ask" in some OO-speak circles) really hit home for me as I think about the kind of stuff I do in procedural langauges I use at work versus the Java/OO stuff I'm learning. (I also liked the writing style quite a bit.)
The GoF book can be pretty intimidating -- I'll admit to having owned GoF for *years* now and not having read/digested certain parts of it. Maybe read DPE first. The other gentle introduction to patterns I would recommend is Martin Fowler's "Refactoring." That introduces you to a couple patterns (hmm, at least Template Method, State/Strategy, and Factory Method) in a sort of backdoor way -- in the course of refactoring code, you move from a non-pattern approach to a pattern approach, so you see the before, the after, the getting there, and some of the decisions/tradeoffs along the way.
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
The GoF book can be pretty intimidating
true... it helped to have an instructor that was willing to give good examples in several different languages (java, c++ and ada95).
John Hembree
hired gun
Ranch Hand

Joined: Mar 07, 2003
Posts: 250

And if it is? Does it hand back a reference to the previously initialized servlet? Is that how it ensures that there's only one instance?
Yes, that's correct, but after reading Michael's comments, I'd have to agree. I passed it with the Singleton as well, but the solution does appear to be along a different strain of singleton's possibly. Not sure if that still qualifies as a singleton, maybe it's a completely different pattern.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
There is more than one way to create a Singleton.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by Greg Harris:
The GoF book can be pretty intimidating
true... it helped to have an instructor that was willing to give good examples in several different languages (java, c++ and ada95).

And don't forget Kyle Brown's Smalltalk design patterns companion book. (Though Smalltalk syntax can be a bit mystifying until you get the hang of it.)
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by John Hembree:
And if it is? Does it hand back a reference to the previously initialized servlet? Is that how it ensures that there's only one instance?
Yes, that's correct, but after reading Michael's comments, I'd have to agree. I passed it with the Singleton as well, but the solution does appear to be along a different strain of singleton's possibly. Not sure if that still qualifies as a singleton, maybe it's a completely different pattern.

This might not be entirely clear from my post, but I was talking about using the singleton as a means of sharing the (, um,) shared data, not anything having to do with "instances" of "initialized servlets." (Not exactly sure what that means in terms of servlets. Only one instance of each servlet gets created. And that and the multiple threads that get generated are managed by the app server. Right?)
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by Marilyn de Queiroz:
There is more than one way to create a Singleton.

Response 1:
Aha. I now understand *exactly*. Retract all claims of red herrings and the like.
Response 2:
Er, um, if there's more than one way to create it, how do you ensure that only one gets created?
[ June 26, 2003: Message edited by: Michael Matola ]
John Hembree
hired gun
Ranch Hand

Joined: Mar 07, 2003
Posts: 250

This might not be entirely clear from my post, but I was talking about ....
Sorry for the confusion, I was answering Pauline's question about the singleton getInstance method earlier in the thread and then commenting on your statement as well.
As for:
Only one instance of each servlet gets created.
I don't think this is entirely true, if you check the code in this thread you will see that multiple instances of a servlet can be created. It would be interesting if the way the instructor implemented the Singleton vs. the way we probably did if it would create separate instances to handle larger loads. I suppose off hand that it wouldn't matter how many instances of the actual class you had, there would be only one reference to the static variable within the jvm. Hopefully I explained all of this correctly.
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67
Originally posted by Pauline McNamara:
And on a related note, I've recently considered buying "Design Patterns Explained". Opinions?

I have DPE. (I got it after hearing Alan Shalloway give a talk). I remember him saying that he struggled with the GOF advice of "find what varies and encapsulate it". In a lot of ways GOF is pretty dense and I've struggled with it. I wish I had the time to go through a patterns study grop experience, but at least DPE gives me a different way to approach patterns.


Richard
N 37 33 W 122 18
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: patterns on the cattle drive