- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Standard Java Singleton 'hack':
public class MySingleton { private static class SingletonHolder { public static final MySingleton INSTANCE = new MySingleton(); }
public MySingleton instance() { return SingletonHolder.INSTANCE; }
private MySingleton() { //constructor stuff } }
The above is thread safe, and will only create an instance when instance() is called. This is because Java classes are loaded on demand, so once MySingleton is referenced, its static field is initialized.
Admin
I think it is supposed to be "Doubleton Patent Pending"
Admin
Session affinity? WTF's that?
:/
Admin
The Gang of Four played the game as well. Quoted from the pattern's consequences section:
"Permits a variable number of instances. The pattern makes it easy to change your mind and allow more than one instance of the Singleton class. Moreover, you can use the same approach to control the number of instances that the application uses. Only the operation that grants access to the Singleton instance needs to change."
So, everyone that thinks the idea could only come from a moron needs to start evaluating whether or not they think the entire concept of patterns is now in question, since obviously the originators were morons.
Admin
That's soooooooo 2005!
We're back to the stateless intarwebs, dincha know?
Admin
Ride BART.
Admin
Eat my shorts!
Admin
The analysis in the article seems a little bit flawed to me. The instance variable in the getInstance() method shouldn't be static, because that's not the variable getting checked in the if statement. And, assuming that Spring won't actually instantiate the PublicationService object until getBean() is called for the first time, the first call to getInstance() will only create one instance of PublicationService, not two.
Of course, it's still broken; the very first call to getInstance is going to return one anonymous instance of PublicationService, and every subsequent call is going to return the Spring-managed instance. But it won't ever instantiate more than two instances, so it's not the rampant memory leak that the article suggests.
The code actually isn't too far from being correct; change the second constructor to be a private static void method and pull the else clause of the getInstance() method out of the conditional and it will work as expected.
Admin
This is the worst C# code I have ever seen. All those c Pound developers need to switch to a real language like vb.
Admin
Admin
For instance, in a simple garbage collected language, things are allocated in heap 1, when collection occurs, objects still referenced are copied to heap 2, heap 2 is now the allocation heap, when collection occurs again, objects are copied to heap 1, and heap 1 becomes the allocation heap again. (This compacts the heap in the process as well)
N-ton pattern is simply a funny name for a circular buffer with N slots...
Admin
All hail the compu-tor!
Admin
Looks like a munged attempt to refactor a singleton as a Spring managed bean.
Admin
I'm on the side that questions the usefulness of singletons, myself. And their little caveat there creates a subtle difference in the original intent does it not? As a singleton, it is a storage point for global variables. As a N-ton, it's just a round-robin corner case.
Admin
viz:
But this is not a good refactoring - it does not maintain the Clearly Documented Feature (TM), namely, that it's ok for the context load to fail. Here, we would get a NullPointerException. Failure to load the context under the original code, resulted in the static context remaining null, and a new services instance instantiated each time the 'singleton' is requested. Hence the "memory hog" reports.
To play well in the face of errors, we might check if the context was initialized:
But this is the same flavour of crap that was initially served up. A better, but more risky fix is to stop caching environmental/startup problems and allow these to bubble up and be handled properly, usually by the application not starting.
I'm sure the coder was trying to be safe, just like those people who clench up in order to fart discretely. But sooner or later, approaches like this will lead to disaster when the silent failures are finally discovered due to some later catastrophic failure and soiled pants.
Failure to load the Spring context is most likely fatal. But if the application really can manage without the Spring instantiated service, as claimed in the code, then why bother having all crap in the first place?
captcha: paratus - one who quotes others
Admin
GoF fanboys scare me IRL.
Admin
Not a hack, an elegant solution (lazy, but performant, so can be used always).
Initialization on Demand Holder in wiki (so it must be true).
Regards
Admin
I'll believe you when you can show me that people actually use the tools.
Also: let's all stop wearing glasses. That way, nearsighted people won't survive to procreate, and no one will need glasses anymore!
Admin
secodn!
Admin
Admin
Actually, considering the topic of doubleton's, I was expecting today's first post to say
Admin
For memory buffer operations that cannot be done in-place, you might want to alternate between two buffers (e.g.: String operations between two string buffers; graphics operations between two surfaces).
Admin
Agreed - it's still bad code, but the WTF analysis is completely wrong. As you say, the first call is broken, but all it needs to fix it is to inline that protected constructor into the 'if' check, and move the 'else' condition to be always run.
Buggy code, sure, but nowhere the near the usual "what lunatic wrote this" standards...
Admin
None of this actually matters, because even getInstance() has no synchronization, and is a race condition.
captcha: venio
Admin
Actually when you really start to ponder it deeply the singleton pattern when dealing with concurrency can become one of the hardest pattern to correctly implement, especially in C++, Java got it a little easier, but there is still lots of room for errors.
Admin
Admin
By the same logic no one should ever drive cars, we should all walk! You know, because people are getting fat/unfit/etc.
We use calculators because they can do simple maths faster and more accurately than we could ever hope to. Let your brain get exercise by doing logic puzzles that are beyond your simple friend the calculator and just like using a car instead of walking, save yourself some time.
Also, for the guys above, please read the definition of the word "irony", you make my soul bleed.
Admin
"The use of words expressing something other than their literal intention!"
Now that IS Irony!
Admin
Admin
I have to wonder how many "doubletons" are running around in Windows right now... is that why MS won't show us the Windows source?
Admin
I know it's pretty obvious but it all makes sense: clearly, the author wanted to target embedded platforms with no file system. Just what you would expect from someone that has hobbies like "training, mentoring and research". He's teh smart!
Admin
His other articles are full of WTFs too http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=1909449 I'd love to know BizTalk just to enjoy the blunders. The last ones http://www.codeproject.com/KB/dotnet/Surrogate_Serialization.aspx http://www.codeproject.com/KB/graphics/gdi_drawingpanel.aspx are enjoyable by a wide public. Our certified hero seems not to be aware of the existence of auto properties.
Admin
The thing that really bugs me is when people use spellcheckers & then pick the wrong word, like roll instead of rôle for example. This seems to be happening more all the time, even in newspapers
Admin
Wow, does -anyone- really understand how Java works?
Based on the discussion so far it really looks to me like none of you really do. Lots of guessing about what this or that may or may not do, no one can agree.
I know nothing about Java/OOP, but it doesn't sound real deterministic to me. I've done a lot of programming in my time (yeah, get off my lawn!) but you all sounds like a bunch gibbering monkeys.
I've got to think that Java itself is the TWTF here.
Brand me a troll, but seriously, WTF?!?!?
Admin
Admin
Admin
Admin
Or were you being ironic there?
Admin
But I'm sure your spelling software would disagree with you about whether "Uhm" is a word, and in this case it would be Spelling software: 1 - You: 0
Admin
Admin
Admin
I have always found this doubleton pattern to be easier to nobulate on an embedded system.
Admin
My son has recently discovered Isaac Asimov, so I've been having a lot of fun reacquainting myself with his work. In his anthology "Robot Dreams", there's a story called The Feeling of Power that suggests a frighteningly believable end result of this trend.
Admin
In illtiz's defence, it could be a portmanteau of "uh" and "um".
Admin
Umm, how about fix it or stfu?
Just add 'static' to the instance() method. Jesus, it's not like it had compilation errors.
Admin
Admin
Your comment boils down to "I admit knowing nothing about the dominant programming paradigm of the past 15-20 years, yet I must inform you how stupid YOU ALL are."
Why should you not be branded a troll?
Admin
Which is OOD in practice.
Admin
You must be a redditor.
Admin
I guess that guy never heard of n-sized object pools. And to boot, he has a master and has won multiple projects. Even more sad, the people in that forum making suggestions on how to improve this abomination.
It ain't a pattern. A pattern is not just some arbitrary class. It does not list motivation (the problem domain), forces, application, collaborating forces, consequences and caveats, intent, all the things a composition should have to be a pattern.
/facepalm.