• 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

Bastard child of singleton ?

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across code of this ilk:


And somewhere in the rest of the code (outside of the main thread! This is a GUI program), it would do this:

Is this a variant of the singleton pattern ? I find it a bit difficult to follow.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is a Singleton variant - and yes, it's a little bit strange, and not what I'd do.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pho Tek:
// the main thread then creates a FooImpl instance and stores in inside a



I would be interested to see how it does this. The code as presented will always return null for Foo.getInstance() and FooImpl.getInstance() ;


Originally posted by Pho Tek:
Is this a variant of the singleton pattern ? I find it a bit difficult to follow.



It would appear to an attempt to make an abstract singleton to use as base class.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something somewhere must do new FooImpl() which would have to do a super() to get that static variable loaded. But I see nothing to prevent something from doing new FooImpl() every five seconds all day long.
[ June 02, 2006: Message edited by: Stan James ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can this be called Singleton or variant.
The basic rule for a creating a Singleton class is having private constructor and static getInstance method which creates the instance if it is null.

As Stan said, you could go on creating multiple instances of Foo from FooImpl.

Try this. It will print two different references which means Foo does not implement Singleton pattern.



Pho, as you said in the original post your doSomething() cannot be in FooImpl class. Try uncommenting "Foo.getInstance.doMore()" in my code, it won't compile. Foo does not know about a method defined in its sub-class.
 
Ranch Hand
Posts: 354
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this should make it a singleton as long as getInstance() is used.
[ June 08, 2006: Message edited by: Jack Ryan ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be really picky ...

The basic rule for a creating a Singleton class is having private constructor and static getInstance method which creates the instance if it is null.



This is the most common implementation in Java, but hardly a rule. If there is a rule, or a way to identify a Singleton in the wild, it's probably to have some mechanism to control creation of instances. There are any number of interesting mechanisms. Per GoF, the rule doesn't even have to specify "exactly one" instance.
[ June 09, 2006: Message edited by: Stan James ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic