What is the better approach to implement Singleton pattern?

March 27, 2008

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?

My answer is “No“. Let us take an example of JRE implementation class Math. Math is a singleton class, but the instance is not exposed as a static method. Rather they made all the methods as static.

JRE exposing Runtime class instance through a static methods instead of keeping all the methods as static because it contains native method, native methods cannot be static.

My recommendation to implement singleton pattern is: make all the methods as static, if for any reason you cannot make all the methods as static, then go for static instance approach.

I feel keeping all methods as static methods is better design and developer friendly.

Entry Filed under: Java. Tags: , , , .

3 Comments Add your own

  • 1. Santhosh Kumar  |  March 28, 2008 at 11:35 am

    Wat if i’m supposed to pass my singleton object to a method. Check out this link

  • 2. S M Humayun  |  March 28, 2008 at 4:36 pm

    IMO static methods approach is not a (better) way to implement singleton pattern. First, you loose the freedom to pass singleton to others. Secondly, you are tightly coupling the singleton code with the calling code. Thirdly, static scope is certainly not a good thing to rely on. See for details.

  • 3. d descombaz  |  March 30, 2008 at 3:16 pm

    To add to the first two posts, you wouldn’t be able to inherit, or implement anything either.

    I think you are describing the preferred way to make static library in Java. Obviously, it depends upon your needs. A singleton, and a non-instantiable class (outwardly non-instantiable at least) with only static methods and/or properties definitely share characteristics, but they are not the same thing.

    Since it doesn’t really create an instance, it increases coupling, and you lose some of the potential benefits of OO design, my feeling is that I wouldn’t generally recommend that someone approaches a singleton this way.

Leave a Comment

hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories

RSS Feed

My Favorite Tech Quote

Complexity is a sign of technical immaturity. Simplicity of use is the real sign of a well design product whether it is an ATM or a Patriot missile.
 
-- Daniel T. Ling

My Favorite Life Quote

எது நடந்ததோ அது நன்றாகவே நடந்தது; எது நடக்கிறதோ அதுவும் நன்றாகவே நடக்கிறது; எது நடகவிருகிறதோ அது நன்றாகவே நடக்கும்.

- பகவத்கீதை