What is the better approach to implement Singleton pattern?

Discussion in 'Java' started by Venkat Sadasivam, Apr 5, 2008.

  1. Singleton is one of the frequently used and easily understandable
    design pattern. Most of the tutorials and books give example of making
    the constructor as private and exposing a static method which will
    provide single instance. The common example they use is Runtime class
    of JRE.

    Is making constructor as private is the only way to implement
    singleton pattern? Is that the best way to implement singleton
    pattern?

    Here is the complete article.
    http://venkatsadasivam.wordpress.co...tter-approach-to-implement-singleton-pattern/

    - Venkat
     
    Venkat Sadasivam, Apr 5, 2008
    #1
    1. Advertising

  2. Venkat Sadasivam

    Steve Guest

    On Apr 5, 7:31 am, Venkat Sadasivam
    <> wrote:
    > Singleton is one of the frequently used and easily understandable
    > design pattern. Most of the tutorials and books give example of making
    > the constructor as private and exposing a static method which will
    > provide single instance. The common example they use is Runtime class
    > of JRE.
    >
    > Is making constructor as private is the only way to implement
    > singleton pattern? Is that the best way to implement singleton
    > pattern?
    >
    > Here is the complete article.http://venkatsadasivam.wordpress.com/2008/03/27/what-is-the-better-ap...
    >
    > - Venkat


    In general the safest way to handle the singleton pattern is to
    construct the object as a static variable and to hide the constructor
    as you have described. Provide a static access method that simply
    returns the static variable.

    Sometime this doesn't work. Say if you have to delay the instantiation
    of the object for some reason - something called lazy initialisation.
    In this case you get into threading issues. These are addressed by the
    'double locking' pattern. Although even that is contentious.

    One big problem with singletons is dealing with multiple class
    loaders. You're fine when dealing with a simple app. Once a web-server
    such as Tomcat gets involved you start having to consider multiple
    class loaders. It's not difficult to see the problem you'll get into
    when you realise a singleton is really specific only to the class
    loader that creates it.

    Then you might consider using something like Spring to handle the
    creation and 'glue' of you objects. Spring provides a sort of object
    registry and can be configured to handle objects as singletons or not.

    Rgds, Steve.
     
    Steve, Apr 5, 2008
    #2
    1. Advertising

  3. In article <>,
    Lew <> wrote:

    > Steve wrote:
    > > In general the safest way to handle the singleton pattern is to
    > > construct the object as a static variable and to hide the constructor
    > > as you have described. Provide a static access method that simply
    > > returns the static variable.

    [...]
    > The double-checked locking pattern is broken, unless synchronized.

    [...]

    Even then, double-checked locking is problematic:

    <http://www-128.ibm.com/developerworks/java/library/j-dcl.html?loc=j>

    John
    --
    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
     
    John B. Matthews, Apr 5, 2008
    #3
  4. Venkat Sadasivam

    Arne Vajhøj Guest

    Venkat Sadasivam wrote:
    > Singleton is one of the frequently used and easily understandable
    > design pattern. Most of the tutorials and books give example of making
    > the constructor as private and exposing a static method which will
    > provide single instance. The common example they use is Runtime class
    > of JRE.
    >
    > Is making constructor as private is the only way to implement
    > singleton pattern? Is that the best way to implement singleton
    > pattern?
    >
    > Here is the complete article.
    > http://venkatsadasivam.wordpress.co...tter-approach-to-implement-singleton-pattern/


    private constructor and private static instance and public static method
    to get instance is the only way to implement the singleton pattern.

    Other methods are not singleton as defined by GoF.

    And you blog post is completely off - Math is not a singleton class.

    A singleton class carry data that should exist in one copy.

    Math is a bunch of static methods but no data.

    In C# you can declare a class as static to indicate that it only
    contains static.

    OOP is good but occasionally problems are really procedural.

    Arne
     
    Arne Vajhøj, Apr 5, 2008
    #4
  5. Venkat Sadasivam

    Arne Vajhøj Guest

    Steve wrote:
    > Sometime this doesn't work. Say if you have to delay the instantiation
    > of the object for some reason - something called lazy initialisation.
    > In this case you get into threading issues. These are addressed by the
    > 'double locking' pattern. Although even that is contentious.


    You do not get into any problems if you use synchronized properly.

    You get into problem if using double locking, because it does not work.

    > One big problem with singletons is dealing with multiple class
    > loaders. You're fine when dealing with a simple app. Once a web-server
    > such as Tomcat gets involved you start having to consider multiple
    > class loaders. It's not difficult to see the problem you'll get into
    > when you realise a singleton is really specific only to the class
    > loader that creates it.


    On the contrary - it would be a disaster if classloaders did not work
    that way.

    Different web apps are supposed to be independent of each other.

    Arne
     
    Arne Vajhøj, Apr 5, 2008
    #5
  6. Venkat Sadasivam

    Arne Vajhøj Guest

    John B. Matthews wrote:
    > In article <>,
    > Lew <> wrote:
    >> Steve wrote:
    >>> In general the safest way to handle the singleton pattern is to
    >>> construct the object as a static variable and to hide the constructor
    >>> as you have described. Provide a static access method that simply
    >>> returns the static variable.

    > [...]
    >> The double-checked locking pattern is broken, unless synchronized.

    > [...]
    >
    > Even then, double-checked locking is problematic:
    >
    > <http://www-128.ibm.com/developerworks/java/library/j-dcl.html?loc=j>


    It is amazing how much broken code and unreadable code has been written
    to try and improve the performance of singleton pattern.

    I don't think I have ever heard of a case where there were a
    performance problem using plain straightforward synchronized.

    Arne
     
    Arne Vajhøj, Apr 5, 2008
    #6
  7. Venkat Sadasivam

    thufir Guest

    On Sat, 05 Apr 2008 02:26:35 -0700, Steve wrote:


    > Sometime this doesn't work. Say if you have to delay the instantiation
    > of the object for some reason - something called lazy initialisation. In
    > this case you get into threading issues. These are addressed by the
    > 'double locking' pattern. Although even that is contentious.


    I don't have it on hand, but in "effective java" by bloch, admittedly
    old, I believe he advocates Singleton as a means of being thread safe.


    -Thufir
     
    thufir, Apr 16, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Kay
    Replies:
    2
    Views:
    421
    dasjotre
    Sep 6, 2006
  2. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,285
    Proton Projects - Moin
    Mar 27, 2007
  3. Wilhelm
    Replies:
    1
    Views:
    172
  4. Trans
    Replies:
    12
    Views:
    284
    Robert Klemme
    Sep 14, 2007
  5. Luke Matuszewski
    Replies:
    53
    Views:
    374
    Thomas 'PointedEars' Lahn
    Feb 17, 2006
Loading...

Share This Page