• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singletons

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a question that just occurred to me today: What is the advantage of creating singleton objects such as

...versus simply using static class members, such as

As long as all of the members are static, there really would be no difference. As long as both supply a private constructor to prohibit instantiation, aren't they both equivalent? The only advantage I can see in the first example is if somewhere down the road the class is converted to a non-singleton class....the api would not have to change. But if you know the class will always be singleton, why do it the first way? You always have to have calls such as

instead of

Anyone have an opinion about this?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,


The only advantage I can see in the first example is if somewhere down the road the class is converted to a non-singleton class....the api would not have to change.


This is consequence #5 from Design Patterns by Eric Gamma et at regarding the Singleton Pattern:
5. More flexible than class operations. Another way to package a singleton's functions is to use class operations (that is, static member functions in C++ or class methods in Smalltalk). But both of these language techniques make it hard to change a design to allow more than one instance of a class. Moreover, static members in C++ are never virtual, so subclasses can't override them polymorphically.
The same thing is true for Java. Just give it a try:

Here's the output:

Michael Morris
SCJP2
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To say it with other words: The singleton pattern is *encapsulating* both the actual number of existing instance and the actual class the instance is of. For example, for testing-purposes it might be handy to return a mock-instance instead of the "real" one.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ilja,
Exactly. You can decide at runtime what subclass to use instead of being tied down to what is esentially a final class.

Michael Morris
SCJP2
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Morris:

Exactly. You can decide at runtime what subclass to use instead of being tied down to what is esentially a final class.
Michael Morris
SCJP2



Subclassing a singleton can be a fatal idea. Lets say you have a singleton class FlieSystem, now some day for some reason you define a subclass EncryptedFileSystem. The probelm is, you can use both FileSystem and EncryptedFileSystem as singleton within a VM and you end up with two intances of FileSystem class. Thats why its always advisable to keep singleton classes final.
If the only entry point to the subclass is thru the singleton method of the superclass(that is, you dont provide or use the getInstance of the subclass), still there are problems. The subclass have to use a nonprivate constructor, also the superclass has to know about its subclass which does not look neat.
Using a singleton rather than using static members has one advantage regarding generalization. Your singleton can implement some other interface, this way classes who know the interface can use it without knowing if the implementation is singleton. It can be very covenient for reuse.
just my 0.2$
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zakaria,
There ain't no problem if you start the Singleton with a Factory and only allow one instance to be created. Any programmer can misuse any class if he wants to, but by using a factory you can, as I said, choose whichever subclass you want at runtime (as the experts Eric Gamma, et al pointed out in their now cult status book Design Patterns).
Thats my dos centavos
Michael Morris
SCJP2
 
Jeff Wisard
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me that subclassing singletons means that the parent is not really a true singleton as defined by the pattern. The parent would have to have a non-private default constructor in order to subclass....and this means that parent MAY have multiple instances running within a single JVM because it can no longer control who creates an instance of itself.
The idea of a factory creating singletons does not make sense to me either. The same rules apply...a non-private constructor is needed. The only way to enforce a singleton in this way is to create a package that contains the factory class and the singleton class only...and have the singleton class's constructor set with package scope. Or, define the singleton class as a private member class of the factory class...but what would the point of that be? If by factory you mean supply a static getInstance() method to the singleton, then that's a different story.
So, to sum up on the original question, it seems that the only real advantage to not using static methods is the ability to implement interfaces in the singleton. A secondary consideration would be the possibility of converting the class to a non-singleton class. In this case, the API for the class does not have to change.
Thanks for the input!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,
And as I said, any programmer (moron?) can misuse the intent of a design. I suggest you pick up a copy of Design Patterns and see what the experts say about Singletons unless you are wiser than than the gurus who wrote it. :roll:
Michael Morris
SCJP2
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Off the top of my head:

This can be subclassed, yet guarantees that there is only one instance of any subclass of AbstractSingleton. So a subclassable singleton certainly seems possible. I don't know how often it's actually useful or necessary, but that's another issue...
[ March 29, 2002: Message edited by: Jim Yingst ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.research.umbc.edu/~tarr/cs491/lectures/Singleton.pdf
 
Jeff Wisard
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Jim and Marilyn. Your code example and the .pdf link were helpful.
Michael, I think you should be careful of your tone when posting.
[ April 01, 2002: Message edited by: Jeff Wisard ]
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic