Thursday 5 December 2013

When to use Singleton or Class with only static methods

Singleton is used when frequently create the instance of the class with same same Data Members or No Data Members and the Constructor is Parameter less only. We always need object with same data member configuration. Also, this design pattern is imminent, when Context or Data Members of the objects are very expensive to create. For example they involve reading or writing from File, Database, Configuration Files, Loading an Image in the memory, Loading Excel File, Invoking a Web Service. We need to use this class
with same data member configuration. We need to use this class in the same Context.
Singleton is also applicable when we use a Third Party or .NET framework provided class but we use it
with the same Context or Configuration of Data Members. It better to have new class that compose the
Used Class and have Singleton mechanism for the newly created class.


Class with static methods is used when the Class has not Data Members or Context at all and the methods are stand alone and they have no dependency on class data members. then it is always best to have static methods.

No comments:

Post a Comment