• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

java singleton implementation choices

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just to check with you guys on java singleton implementation, basically I have two choices,

1. a Class containing all public static operations. downside is that it can not be extended since static method will not be inheritated. This is not really a downside since singleton is not really meant to be subclassed

2. a Class contains normal operations but with private constructor and provides a static getInstance method....

so which one is better? any insights?

or simply they are simply the same and use as per developer's preference?

thanks
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. a Class containing all public static operations. downside is that it can not be extended since static method will not be inheritated. This is not really a downside since singleton is not really meant to be subclassed


This is not a singleton, since you do not have any instance of the class. And your statement is wrong - static methods are inherited:


2. a Class contains normal operations but with private constructor and provides a static getInstance method....


This is indeed a singleton.


Both are viable solutions, but to different problems:

Use a singleton when you need different classes to access the same information - a good example is mapping resources to share between different objects.

Use static methods when you want to perform identical operations on similar objects in different classes - a good example is converting from one class to a different one, like converting a Collection to a String of comma-separated values.
[ November 16, 2006: Message edited by: �dne Brunborg ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I you have a class with all methods as static , then that becomes a utility class.All standalone methods for specific purposes.In those cases there should not be any variable at class level other that final.

In prefer using singleton in case where I have a bean like class containing certain readonly configuration information.So in that case I do not need a more than one object for that.Or any other part of the application which needs some information from the configuration can get a handle to the singleton config object and get the required property from that.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both utility classes and Singletons have some serious drawbacks (which they share with the Monostate pattern), that make me prefer the Just Create One pattern in most cases.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The monostate pattern is generally to be avoided, IMOH. The Singleton is more useful, I tend to use it to keep track of EJBHome instances (thus needing to do the JNDI lookup only once per Home interface).

The Just Create One pattern not to be scorned - remember the KISS principle
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meng Yi, you can also read the recent discussion here.
[ November 16, 2006: Message edited by: Jim Yingst ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the few months I have been programming. I have used the class structure to program. So thats basically all I know. Which works out Really well. Go for the class.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic