• 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

SingleTonpattern and Static methods

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Could anybody say the difference between singleton pattern and static methods in class.

When we have to use each one.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singletons usually hold some state (variables) and you want to be sure there is only one copy in the JVM. Caches are good examples. I stuff data in there for use later because it's faster to use memory than a database or disk, and I want to be sure there is only one copy of the data.

Static methods often operate on the parameters only and don't use internal state. The Math class methods are good examples.

Sometimes we blend these things. For example static logger methods might use a singleton log formatter object.

Both Singleton and Static Method designs have some negatives. They often bind you closely to the implementing class, making it hard to subclass them or plug in different implementations. Look carefully for alternatives before you invest too much in these techniques.

Hope that helps! Wander down to the OO, UML, etc. forum for lots more conversation like this.
[ July 14, 2005: Message edited by: Stan James ]
reply
    Bookmark Topic Watch Topic
  • New Topic