• 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

singletondesign pattern

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are u ever used singletondesign pattern in ur project tell me what is the situation?
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most common use is in web tier ServiceLocators.
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Examples include looking up EJB Home, getting Logger.

The Runtime class in Java is also a singleton.
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "trick" with singleton is to not over-use it. A common mistake in application design comes from thinking "gee, I don't think I'll ever bother creating more than one of these, so I guess I should use singleton". The whole point of singleton isn't just that you only see yourself using one instance. It is that fundamentally your app would be broken if you tried to use more than one instance, and that the implementation of that instance could vary (e.g. by subclassing or creating multiple implementations of an interface).

Objects representing external resources like your operating system services (java.lang.Runtime) or windowing environment, or sometimes an object creation factory tend to make for good examples of using singleton effectively. Karthi's example of Logger is a good example, particularly if you think in terms of the Jakarta commons logging API instead of Log4J. It isn't the Logger instance itself which is the singleton, it is really the logging implementation behind it that is the true singleton. The implementation could be Log4J or Sun's logging API or something else. By changing the configuration of commons logging, all your code would get a new logging environment without any code change.
[ January 19, 2006: Message edited by: Reid M. Pinchback ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic