File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Regarding Singleton design pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding Singleton design pattern" Watch "Regarding Singleton design pattern" New topic
Author

Regarding Singleton design pattern

Raj chiru
Ranch Hand

Joined: Aug 12, 2008
Posts: 141
Hi Friends,








Has anybody else here read the above two ways of singleton design and if so, what are your thoughts Regarding two designs?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Too difficult a question for "beginning". Moving thread.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Both implementations are broken. See a previous, horrendous thread on singleton:

http://www.coderanch.com/t/473794/Beginning-Java/java/Static-vs-Single-Pattern
marten kay
Ranch Hand

Joined: Feb 03, 2007
Posts: 178

Dear Raj

I had a quick read of the Static-vs-Single-Pattern thread and agree that it is pretty horrendous.

To borrow from the religious tone of that thread, my 'bible' is Joshua Bloch's Effective Java (2nd Ed). On page 17 Joshua details three ways of creating a singleton,
  • final field
  • static factory, and
  • use an enum with one element (form 1.5 only)

  • I recall the Head First Patterns book also has an example in it that deals with the double locking.

    Anyway, while only a beginner, I am using a couple of Singletons. I can't see an alternative for maintaining system state across the application.

    Marten


    when in doubt put it in parenthesis and stick a dollar sign in front of it, only good can come from this.
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    I think Simpletons are necessary sometimes--the trick is to actually do them right (surprisingly difficult), and make sure that references to them are injected, not hard-coded. Double-locking is pretty broken: not as bad as it was pre-1.5, but still broken (see the IBM link provided by Janeice and my followups).

    I never manage Simpletons myself; I let Spring do it. I think enums are the best post-1.5 way of implementing non-Spring-esque singletons, although I'm not convinced that's really an intended enum usecase :/
    swapnl patil
    Ranch Hand

    Joined: Aug 13, 2007
    Posts: 80


    According to me Second approach is right implementation of singleton pattern . Please Go through Head first Singleton pattern book.
    Raj chiru
    Ranch Hand

    Joined: Aug 12, 2008
    Posts: 141
    Hi Friends,
    Thanks For Your Replies.

    But, we have any drawbacks with eagerly created instance(First code)?
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Yes, it's not thread-safe.
    Raj chiru
    Ranch Hand

    Joined: Aug 12, 2008
    Posts: 141
    Hi David,
    Thanks For Your Reply,

    But with eagerly created instance, the JVM create the unique instance of the singleton when the class is loaded.
    The JVM guarantees that the instance will be created before any thread access the static singletonObject variable.
    so,it is thread-safe,Right?
    Marilyn de Queiroz
    Sheriff

    Joined: Jul 22, 2000
    Posts: 9052
        
      12
    I think the first is more thread safe than the second.


    JavaBeginnersFaq
    "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
    Raj chiru
    Ranch Hand

    Joined: Aug 12, 2008
    Posts: 141
    Hi Marilyn,
    Thanks For Your Reply.
    But i think most of the people implementing the sinleton with Lazy instance creation,why?

    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Because at one point people thought double-locking worked (and it's less-broken in Java 1.5+, but still broken). Again, see the previous, horrendous singleton thread and read the references Janeice and I provided.
    Raj chiru
    Ranch Hand

    Joined: Aug 12, 2008
    Posts: 141
    Hi David,
    Thanks For Your Reply.
    David Newton wrote:Because at one point people thought double-locking worked (and it's less-broken in Java 1.5+, but still broken). Again, see the previous, horrendous singleton thread and read the references Janeice and I provided.


    can we solve the "double-checked locking" with ThreadLocal class?
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19760
        
      20

    With ThreadLocal you do not have one singleton instance but one instance per thread.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Raj chiru
    Ranch Hand

    Joined: Aug 12, 2008
    Posts: 141
    Hi Rob,
    Thanks For Your Reply,
    I have one more doubt,what happens when singleton class implements serializable interface?
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19760
        
      20

    Unless you implement readResolve you will be able to make a clone of the object. With readResolve you can prevent this:
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Regarding Singleton design pattern