• highphilosopher (unregistered)

    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.

  • (cs)

    "Always there are two."

  • Neville Flynn (unregistered)

    First rule in government spending: why build one when you can have two at twice the price?

  • (cs) in reply to dpm
    dpm:
    "Always there are two."
    You, sir, just made my day!
  • Megatron (unregistered)

    You have got to love OOD, pure gibberish.

  • Ed (unregistered) in reply to highphilosopher
    highphilosopher:
    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.

    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.

  • Anon (unregistered)

    There can be only two (...or more).

  • Jumble (unregistered)

    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.

  • Verm (unregistered)

    "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!

  • (cs)

    Someone actually "published" an article on the Doubleton pattern. Needless to say it is full of WTFs and serves no useful purpose.

  • Johnny Awkward (unregistered)
    TeKilla:
    frist

    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.

  • Tim (unregistered) in reply to Ed
    Ed:
    highphilosopher:
    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.

    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.

    I think there are often cases when you would say "in the context of this particular application there will only ever be 1 instance" but that doesn't mean the class itself should inherently have singleton behaviour because some other application might have a legitimate reason to create several.

    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.

  • (cs)

    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.

  • Jim (unregistered)

    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

  • Steve A (unregistered)

    Who is Kristopher talking to and why does he spell his name with a 'K'. Is he from Russia?

  • Anonymous (unregistered)

    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).

  • True that (unregistered)

    "What, no Ten-ton weights?"

  • Anonymous (unregistered) in reply to Jim
    Jim:
    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.

    Do you ever consider that maybe, just maybe, the reason we're all so shit at spelling these days is because of an over-reliance on those automated tools? Maybe the only way we'll be safe from typos is if we stop relying on those tools and start relying on our brains.

    It's a pipe dream, I know.

  • Mr. S. (unregistered)

    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?

  • Me (unregistered)

    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.

  • Brian (unregistered)

    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.

  • (cs) in reply to Brian
    Brian:
    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.

    They should have made the getInstance() method non-static so that they could subclass it to return different instances.

  • MemoryHog (unregistered)

    What do Java and JoAnn Fabric's have in common?

    Patterns.

  • Susie (unregistered) in reply to frits
    frits:
    Someone actually "published" an article on the Doubleton pattern. Needless to say it is full of WTFs and serves no useful purpose.

    Is it a joke? He doesn't even try to defend it, sarcastically or otherwise.

  • Anonymous (unregistered) in reply to frits
    frits:
    Someone actually "published" an article on the Doubleton pattern. Needless to say it is full of WTFs and serves no useful purpose.
    I read that article and honestly thought it was a joke at first. The author makes no attempt to justify the "pattern" or give any suggestions as to how or why it might be used. The code is utterly trivial and does nothing of use. God only knows how this article was accepted by CP. My favourite part is in the "about the author": he "spends his free time travelling in the BART (Bay Area Rapid Transit) in Fremont, CA". WTF? He spends his free time riding public transport?

    I think the linked CP article might actually be funnier than today's WTF, thanks frits!

  • Ken (unregistered) in reply to tiller
    tiller:
    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.

    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

  • Matt Westwood (unregistered) in reply to dpm
    dpm:
    "Always there are two."

    You mean: "Always two, there are."

  • Anonymous (unregistered) in reply to Susie
    Susie:
    frits:
    Someone actually "published" an article on the Doubleton pattern. Needless to say it is full of WTFs and serves no useful purpose.

    Is it a joke? He doesn't even try to defend it, sarcastically or otherwise.

    If it were a joke he wouldn't be trying to defend himself in the comments:

    Doubleton Man:
    "There is no use of talking like that. It is of no help."
    It is of no help, just like your ridiculous Doubleton class! If that's what 12 years of experience buys you I'm quitting IT after year 10.
  • highphilosopher (unregistered) in reply to Johnny Awkward
    Johnny Awkward:
    TeKilla:
    frist

    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.

    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:

    1. Read the WTF
    2. think of posting FRIST
    3. Type FRIST in the comment box.
    4. Turn off the computer
    5. Smash the computer with a hammer
    6. Get another computer and go back and read the rest of the comments replying as needed.
    7. Repeat the next day

    True this is an expensive process what with completely ruining a computer every day, but WOW the irony we enjoy is SO WORTH IT!!!

  • Paula (unregistered)

    There can be only brillant.

  • (cs) in reply to Anonymous
    Anonymous:
    frits:
    Someone actually "published" an article on the Doubleton pattern. Needless to say it is full of WTFs and serves no useful purpose.
    I read that article and honestly thought it was a joke at first. The author makes no attempt to justify the "pattern" or give any suggestions as to how or why it might be used. The code is utterly trivial and does nothing of use. God only knows how this article was accepted by CP. My favourite part is in the "about the author": he "spends his free time travelling in the BART (Bay Area Rapid Transit) in Fremont, CA". WTF? He spends his free time riding public transport?

    I think the linked CP article might actually be funnier than today's WTF, thanks frits!

    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.

  • Bryan The K (unregistered)

    I get it, the real WTF is that they are using Java right?

    CAPTCHA: incassum - Incassum zombies, get the shotgun

  • DeepThought (unregistered) in reply to frits
    frits:
    Someone actually "published" an article on the Doubleton pattern. Needless to say it is full of WTFs and serves no useful purpose.

    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.

  • Anonymous (unregistered)

    Singleton...doubleton...amateurs.

    My patterns go to 11.

  • Jim (unregistered) in reply to Anonymous

    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.

  • (cs) in reply to Neville Flynn
    Neville Flynn:
    First rule in government spending: why build one when you can have two at twice the price?

    One of my favourite movies!

    "... wanna take a RIDE?"

  • Wheaties (unregistered) in reply to frits

    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."

  • (cs) in reply to Megatron
    Megatron:
    You have got to love OOD, pure gibberish.

    SpectateSwamp, is that you?

  • sino (unregistered) in reply to Megatron
    Megatroll:
    You have got to love OOD, pure gibberish.
    FTFY
  • ShatteredArm (unregistered) in reply to Ken
    Ken:
    tiller:
    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.

    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

    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.

  • sino (unregistered) in reply to DeepThought
    DeepThought:
    frits:
    Someone actually "published" an article on the Doubleton pattern. Needless to say it is full of WTFs and serves no useful purpose.

    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.

    Ok. I'll play your little game.

    Perhaps it could be of use in load-balancing requests over a stateless protocol?

  • Anonymous (unregistered) in reply to Jim
    Jim:
    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.

    Very sad unfortunately. So many people seem to forget the proven fact that brains need excercise to work at their fullest capacity. By transferring those small, day-to-day mental exercises (like arithmetic) to a computer or gadget, we lose a very important part of our brain's "daily workout".

    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.

  • Anonymous (unregistered) in reply to sino
    sino:
    DeepThought:
    frits:
    Someone actually "published" an article on the Doubleton pattern. Needless to say it is full of WTFs and serves no useful purpose.

    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.

    Ok. I'll play your little game.

    Perhaps it could be of use in load-balancing requests over a stateless protocol?

    That would be a very naïve load balancing strategy so I don't think it really counts as a valid use (not for any intelligent person, at least). Any more suggestions? We'll find a home for that useless code eventually.

  • Jay (unregistered) in reply to Jim
    Jim:
    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.

    Sure. Like I always say: If the compiler didn't find any errors, the code must be right! The computer said so.

  • Jay (unregistered)

    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.

  • (cs) in reply to Ken
    Ken:
    tiller:
    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.

    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

    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.

  • TeKilla (unregistered) in reply to Johnny Awkward
    Johnny Awkward:
    TeKilla:
    frist

    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.

    I tried that, but -- even though my browser is a singleton -- after I closed it, it was still there. So I figured I better post it to be sure.

  • Patrick (unregistered) in reply to Johnny Awkward
    Johnny Awkward:
    TeKilla:
    frist

    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.

    The misspelling isn't ironic, it's just dumb.

  • somedudenamedbob (unregistered) in reply to Anonymous
    Anonymous:
    Jim:
    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.

    Do you ever consider that maybe, just maybe, the reason we're all so shit at spelling these days is because of an over-reliance on those automated tools? Maybe the only way we'll be safe from typos is if we stop relying on those tools and start relying on our brains.

    It's a pipe dream, I know.

    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?

  • KingOfIrony (unregistered) in reply to Johnny Awkward

    frist!

Leave a comment on “The Doubleton Pattern”

Log In or post as a guest

Replying to comment #:

« Return to Article