Static Class vs. Singleton Class

I have explained two different implementation for making a class to Singleton, means restricting no of instance of a class to only one.You can refer previous posts Singleton Design Pattern by example & Alternative implementation for Singleton Class.

Here by Static Class means a class that’s has only public static methods. Two different implementations as per previous post for restricting class instances to only one are making all constructor private referred as  Singleton  class and declaring all public methods as static. 

Singleton class has several advantages over Static class pattern

1. A singleton class can extends class and implement interfaces , while Static class can not.

2. A singleton can be instantiated lazily or asynchronously while a static class is generally initialized when it is first loaded.

3.A singleton class can extended and its methods can be overridden.

4. The most important advantage is the singleton can be handled polymorphic ally without forcing their users to assume that there is only one instance.

Comments

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester