CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, April 05, 2010

Advantages of Singleton Class over Static Class?

Point 1)
Singleton
We can get the object of singleton and then pass to other methods.
Static Class We can not pass static class to other methods as we pass objects

Point 2)

Singleton
In future, it is easy to change the logic of of creating objects to some pooling mechanism.
Static Class
Very difficult to implement some pooling logic in case of static class. We would need to make that class as non-static and then make all the methods non-static methods, So entire your code needs to be changed.

Point3:)
Singleton
Can Singletone class be inherited to subclass? Singleton class does not say any restriction of Inheritence. So we should be able to do this as long as subclass is also inheritence.There's nothing fundamentally wrong with subclassing a class that is intended to be a singleton. There are many reasons you might want to do it. and there are many ways to accomplish it. It depends on language you use.
Static Class
We can not inherit Static class to another Static class in C#. Think about it this way: you access static members via type name, like this:
MyStaticType.MyStaticMember();
Were you to inherit from that class, you would have to access it via the new type name:
MyNewType.MyStaticMember();
Thus, the new item bears no relationships to the original when used in code. There would be no way to take advantage of any inheritance relationship for things like polymorphism.

Post a Comment