Singleton vs a class with full of static methods
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Vijayy_kumarr
Posted On:   Monday, April 26, 2004 05:54 AM





Can any body tell me the pros and cons associated with each


case using a singleton and using a class with full o static


methods interchangebly

Re: Singleton vs a class with full of static methods

Posted By:   kendrick_wilson  
Posted On:   Saturday, May 1, 2004 01:57 PM

Please read pg 128 in Design Patterns:Elements of Reusable Object Oriented Software......

It is discuss in consequence number 5.

Re: Singleton vs a class with full of static methods

Posted By:   Sean_Owen  
Posted On:   Monday, April 26, 2004 12:38 PM

The singleton approach is a little more complicated, and takes some care to implement correctly in Java. You have the slight overhead of retrieving the singleton instance.

However the singleton approach is a little more flexible and object-oriented. For instance, by using a singleton, you leave open the possiblity of returning subclasses of the singleton class from the getInstance() method. That probably won't come up, but the extra abstraction does buy you that small extra degree of flexibility.

If your code needs to allocate and keep a reference to some resources, like a database connection, then a singleton is probably preferable because you can "lazy initialize" easily -- you can wait until the singleton is actually used to allocate the resources. It would be harder to do this with a class full of static methods.
About | Sitemap | Contact