This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SingleTonpattern and Static methods

 
KasiMurugan Ramasamy
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic