OK, This question came up at work, and I was curious to see if there were any strong opinions out there. ... class Singleton { static { // init } static public...
#1 is not really a singleton. Static methods are not very plugable and don't get the benefits of a singleton in many cases. For instance, lets say that you...
Shoemaker, Michael (S...
mshoemaker@...
Feb 6, 2002 6:56 pm
292
... and ... a ... are ... do ... saying ... of I'll say it, then: :-) Static initializers that do any non-trivial work (i.e., anything that could possible...
It all comes down to when you create your instance. In method #1, you create it in the static initializer. I'm not sure I agree with the debugging issues; I've...
Oh, yeah, and throwing Exceptions in static initializers is not permitted (if you want your app to continue to run). From my experience, if something fails in...
From: "crazyboblee" <crazybob@...> ... Go write some code that throws an exception during static initialization, then try to debug it. For extra fun,...
... Runtime exceptions can always be thrown, and produce the delightful stack traces I mentioned before. ... Indeed, often in hard to track down ways. [ Kyle...
Thanks for the replies so far. I am amused that what everybody has attacked so far is the static initialization, which I put in to my example just for ...
Louis, This is an issue that I've bounced back and forth on, but have finally adopted #2 as my preferred method for coding a singleton. My reasons are...
... Um, wouldn't the class being unloaded wreak equivalent havoc on example #2's shared static instance as on #1's static variables? Wouldn't the reload of the...
Hi Louis, I always like to put some context in with a real world example. My favorite singleton is one that gets all the properties of the system. Here would...
I don't think the class variable representing the singleton instance represents the state of the singleton, but is just a handle to it. If any object in the...
Gang, You all have very good points. I think that the one thing about the #2 singleton pattern that was not mentioned was the flexibility to use polymorphism...
... multiple ... because ... First, you obviously have no idea how much experience I have writing code that uses static initialization. Second, those of us who...
This is a good example, but there is no need for your client to be exposed to these details. Using getInstance() and polymorphism does not allow you to extend...
From: "crazyboblee" <crazybob@...> ... I would guess the answer is "a whole lot" from your comment, though :-) ... I've not been bitten by such a bug...
... could ... It really depends on the nature of the solution. If the component is not critical, I'd print a warning and continue. If the application depends...
Correct me if I'm wrong, but based on your description, if the Singleton class's ClassLoader can be reclaimed, than no client code would be left that could...
This is true and it does happen. The only time I've seen this happen "in the wild" is when another program on the machine running the JVM had a memory leak...
It sounds to me like there was something else going on here. Maybe they were using a WeakHashMap or something? The VM shouldn't clear strong references under...
OK all you Test-First gurus. You talk a good story, and I'd like to believe you that test first is The Way. I want to do it. But here's my real life problem: I...
I'm anything but a Test-First guru, but I do have a couple ideas. If you did a little refactoring and broke out a couple more methods, it would be easier to...
Another try. I don't know why the message wasn't forwarded the first time, but here is the text: Date: Mon, 11 Feb 2002 15:57:19 -0000 From: "paul_t_craig"...
... Louis> OK all you Test-First gurus. You talk a good story, and I'd Louis> like to believe you that test first is The Way. I want to Louis> do it. But...
The task, as I originally conceived of it, is this: The thread's job is to open a server socket and then loop forever accepting client connections. For each...
Bob, thanks for the comments! They actually gave me some ideas, but I've got a few questions about what you said. The refactoring that you suggested is already...
... Louis> The task, as I originally conceived of it, is this: The Louis> thread's job is to open a server socket and then loop Louis> forever accepting client...