- 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
The singleton pattern is usually the hardest for people to implement properly. I've never understood why. It's not a hard pattern, but sometimes people don't think through their code before they commit it.
Admin
"Always there are two."
Admin
First rule in government spending: why build one when you can have two at twice the price?
Admin
Admin
You have got to love OOD, pure gibberish.
Admin
I think that's often because when most people think they need a singleton it turns out they've just designed the system badly. I can only think of a couple of examples where it makes sense to use singletons off the top of my head (connection pool, cache. . .)
Bad design => bad coder. Bad coder => can't implement a singleton for poop.
Admin
There can be only two (...or more).
Admin
On a related note, I find it hilarious(?) when you see people who are fervently against global variables but will happily make a singleton of any class they only seem to need one of at the time they write it.
Admin
"The best part," Kristopher added, "the class says that it's a Singleton, but the first time you call getInstance, it has to create 2 instances."
Actually... the best part is:
@And of course, by default, Spring makes all beans Singletons, so there's no need for this crap at all."
I actually lol'd at this comment!
Admin
Someone actually "published" an article on the Doubleton pattern. Needless to say it is full of WTFs and serves no useful purpose.
Admin
Congratulations. You've got the ironic misspelling, but these days it's customary to ironically leave your first post until later in the conversation as well, thus making the post even more ironic.
However, I'd like to propose that, in order to increase the irony even more, we type "frist" into the comment box and then close the browser before clicking on submit. That would be so ironic we just wouldn't be able to help but think what an amazing person you are, and laugh at your comic genius.
Admin
For instance, a migration tool might want to have 2 connection pools to shovel data from one system into another.
What's why I normally prefer to have the singleton behaviour in a separate "singleton factory" rather than having a private constructor in the class itself.
Admin
The instance variable should not be static, because the static variable which store the value is pubContext. instance is just used to pick the right one. (Bad implementation)
So assuming that PublicationService store the instance in a way that later can be retrieved with instance = (PublicationService) pubContext.getBean("publicationService");
I don't se how there can ever be exactly 2 instances.
The public do nothing constructor is just stupid.
Admin
I really like that the article is called the Doubleton Patten.
Just goes to show no one is safe when it comes to spelling errors that are not picked up by automated tools.
-- Note from Alex: facepalm
Admin
Who is Kristopher talking to and why does he spell his name with a 'K'. Is he from Russia?
Admin
It always surprises me how often you see this basic pattern implemented incorrectly. It is a very simple concept that is very simple to implement but people screw it up all the time.
Personally I love this pattern. There is something beautiful about its simplicity, I can't even quite put my finger on it. I'm going to go apply this pattern right now to a repository I'm working on (which uses the repository pattern - yes, I do love a good pattern).
Admin
"What, no Ten-ton weights?"
Admin
It's a pipe dream, I know.
Admin
OK, I don't get why this is bad. The getInstance either calls the constructor with a file to get configuration data from or it uses a stored instance. getInstance does not actually store the instance, so I assume that would happen somewhere else. It seems to be more of a possibly cached object than a singleton.
Or am I just not understanding how Spring works?
Admin
My guess is that this used to be a Singleton until they refactored to use Spring, but didn't understand how to change the rest of the code to use the Spring-provided PublicationService using dependency injection.
Admin
This looks like Spring... Which makes this somewhat less of a WTF as the Spring container will manage the singleton instance of the bean.
You could call getInstance() as many times as you want and the container would always return the same object.
Bad, unclear code? yes. Broken singleton? probably not.
Admin
Admin
What do Java and JoAnn Fabric's have in common?
Patterns.
Admin
Is it a joke? He doesn't even try to defend it, sarcastically or otherwise.
Admin
I think the linked CP article might actually be funnier than today's WTF, thanks frits!
Admin
There's always two instances because the one created by Spring will not go away and the getInstance method will always create a new instance since the variable being checked for null is already null.
Thus 2 instances 1 in Spring 1 created by getInstance
Admin
You mean: "Always two, there are."
Admin
Admin
My aren't you behind. We DID do this for awhile, but decided that simply thinking about typing "frist" into the comment box and closing the browser was the next step in the chain of irony that we skipped it.
Now any TDWTF reader who is worth their salt follows these steps:
True this is an expensive process what with completely ruining a computer every day, but WOW the irony we enjoy is SO WORTH IT!!!
Admin
There can be only brillant.
Admin
I didn't even catch the hobbies section. That's funny. I also thought it was a joke at first. Sadly, I don't think it is. When asked why all calls to GetInstance() after the first two return the first instance, the author comes up with a genius fix: He rewrites the code to "equally distribute" the instances arbitrarily. Wow.
Admin
I get it, the real WTF is that they are using Java right?
CAPTCHA: incassum - Incassum zombies, get the shotgun
Admin
Looks like a solution looking for a problem. You've got to love the fact that calling Doubleton.Instance will alternate the results between the two different instances. I can't for the life of me imagine where this would be welcome behavior in an application.
Admin
Singleton...doubleton...amateurs.
My patterns go to 11.
Admin
Indeed I do. I also note that fewer people are able to do arithmetic these days because most of us have a calculator on our phone, so 'why do we need to know how?'. In a similar way, we all assume that, if there is no red squiggly line under a word, then it must be correct, so why check it.
I have to disagree though. I believe the only way we will be safe from typos is to improve the tools we already use. No one wants to have to rely on their own brain when there are adequate, easier to use alternatives. Sad, but true.
Admin
One of my favourite movies!
"... wanna take a RIDE?"
Admin
And now my world only gets smaller as I crouch into a corner crying. I am, however, consoled by the fact that if I ever need to publish an article for a bonus I can come up with the "Tripleton."
Admin
SpectateSwamp, is that you?
Admin
Admin
I'm not so sure the variable checked for null will always be null. I believe this would happen:
First, you call getInstance(). pubContext is null, so it calls the protected constructor. It is important to note here that the protected constructor creates a context object and assigns that to the static pubContext. It then returns the instance of PublicationService created by the protected constructor.
The second time you call getInstance(), pubContext has now been set. Thus, it accesses the pubContext object that was assigned in the previous call to the protected constructor. It is clear that the protected constructor is called just once, as only the getInstance method can call it. What isn't clear to me is whether that public constructor can be called, but that public constructor does nothing, so the singleton variables will be left unchanged.
It seems possible that there could be multiple instances of PublicationService, but it only has static variables, which are only assigned in the protected constructor, which is called just once.
Admin
Perhaps it could be of use in load-balancing requests over a stateless protocol?
Admin
Fun fact: my handwriting is comparable to that of the average 12 year old (just my handwriting - the content is all very intelligent!). This is because I was about 12 years old when I convinced my teachers to let me submit work that was word-processed instead of hand written. I've been using a computer ever since and unsurprisingly, my handwriting has never improved. I can live with bad handwriting but when it gets to the point that people can't do simple arithmetic then society has a problem.
Admin
Admin
Sure. Like I always say: If the compiler didn't find any errors, the code must be right! The computer said so.
Admin
I can imagine any number of problems where you would want exactly two of some object -- a game with two players might have two Player objects, a system to count votes in your state might have two FederalSenator objects, etc -- but I'm hard-pressed to think of an application where you would rely on wanting to retrieve them with a function that first gets #1, then #2, then #1, then #2, etc. I would think it would be far more likely that you would want to have getInstance(1) and getInstance(2) or iterate through them.
The only example I can think of is the same that a previous poster mentioned: some sort of simple load-balancing scheme.
Personally, I prefer to solve the problems I actually have, rather than coming up with solutions and then looking around for problems that they might fit.
Admin
Ok so my assumption that "assuming that
PublicationService store the instance in a way that later can be retrieved with instance = (PublicationService) pubContext.getBean("publicationService");"
Was wrong. Now this code make even less sense but that is ofcause to be expected on thedailywtf.com.
Admin
Admin
The misspelling isn't ironic, it's just dumb.
Admin
It starts there, and before you know it the world is cured of WTFs. What do you want Alex to do in his spare time?
Admin
frist!