Results 1 to 12 of 12
  1. #1
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default Singleton considered stupid, Java and complexity

    In another thread, I pointed out that the Gang of Four Patterns book has encouraged thousands if not millions of programmers to use the Singleton pattern in Java applications.

    This is bad for many reasons. Google for "singleton considered stupid" for a starting set.

    A bigger problem, IMHO, is that it encourages non-OO design, and that is way bad.

    Since Java is an OO language, how come so many applications are written with little or no thought to the OO style and guidelines?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Even I think that singleton is bad in practice. But it is on my knowledge on it. But I know few people, including few of my friends, heavily used singleton and do there works perfectly.

    But what the bad thing there I can see is all about OO. Actually most of the code(Java classes) violate OO concepts massively. I don't think it's good practice. Any OO language should follows OO concepts as much as possible. I like to move with it.


  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    heavily used singleton and do there works perfectly.
    It works, assuming you use the known tools to avoid double check lock bugs.

    But a singleton is just global data. It breaks, fundamentally, the concept of encapsulation.

    It makes testing with JUnit much harder. And testing with JUnit (or something else) is critical to professional software.

    It increases coupling.

    And nearly 99% of the time, its just not needed. When you have problems, well documented problems, why use it. Kind of like juggling running chain saws. Sure, some folks can do it, but its a really bad idea.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I agreed with you. And I don't want to use it anywhere in my applications too. :)

    I really like to work with Encapsulations. Up to now I have changed the functionality of my code thousand times. If I've not followed OO concepts in good way, in big trouble at all.

    Because of that in the past I start to work on OO concepts in more depth, decide to avoid singleton as much as possible.

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I put all my code into the main method that way its all in one place making it easier to read. If you dont sense the sarcasm stop reading. Any ways oo is good for most projects but somethings a programmer doesn't have time to figure a way around the problem.
    My IP address is 127.0.0.1

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Zosden View Post
    oo is good for most projects but somethings a programmer doesn't have time to figure a way around the problem.
    OO is wonderful for some problems, not so good for others.

    But if the programmer isn't smart enough to figure out a solution in the time allowed, maybe they should practice saying "you want fries with that?"

    Or find a job with a smarter PHB.

    The key point is that the Gang of Four practice is out dated and is bad, OO or not-OO.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Amazing. If all added in the main method, how terrible to read the code. I mean all in one place always make a trouble. Just think, how nice if we separate similar functionality in different locations. I'm just say this on common sense, not in OO concepts.

  8. #8
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga
    Amazing. If all added in the main method,
    I think you may have missed the sarcasm alert.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    No I'm not, I just say what I feel on that post. Because I know lots of people use in that way. I found more people here in our forum too. Some members put there code here, with only one method(that is the main method) and it contains around 100 of lines.

    I never look at that code, because it's a mess for me. Really difficult to read the post until the end.

  10. #10
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    LOL most of those post are from people who have just started. Besides who would put anything in one method when its much more fun to put each line in separate methods.
    My IP address is 127.0.0.1

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Because everyone has there own passion of coding. As you said people used 100 of methods in a single class, with few line of code. Some are in other way around. :)

  12. #12
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

Similar Threads

  1. a really stupid question
    By SwinGirl in forum NetBeans
    Replies: 10
    Last Post: 06-25-2008, 09:06 PM
  2. Replies: 0
    Last Post: 03-29-2008, 12:41 PM
  3. Quick Stupid Question
    By bluekswing in forum New To Java
    Replies: 7
    Last Post: 01-08-2008, 06:35 PM
  4. Replies: 0
    Last Post: 09-26-2007, 08:32 PM
  5. singleton pattern
    By Peter in forum Advanced Java
    Replies: 1
    Last Post: 07-09-2007, 04:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •