- A single constructor, which is private and parameter less. This prevents other classes from instantiating it.
- The class is sealed, though this is option or unnecessary, but may help the JIT to optimize things more.
- A static variable which holds a reference to the single created instance.
- A public static means of getting the reference to the single created instance, creating one if necessary.
Difference between the two
- There is no functional difference between the following
//calling the singleton class method
- Singleton means “single object across the application life cycle”. The scope of singleton is at application level.
- Static means “We cannot have any object pointer”. The scope of static is at App Domain level.
- The benefit of Singleton class is it is much more flexible than static classes. Your code will be more flexible if you use a singleton. The code that uses the singleton doesn’t need to know if it is a singleton or a transient object. On the other hand if we are using a static class means we have to call static methods explicitly.
- Dependency Injection is one of the primary requirements while designing decoupled applications. Static classes once defined could not accommodate any future design changes as by design static classes are rigid and cannot be extended. Singleton on the other hand provides flexibility and also provides sort of a mechanism to control object creation based on various requirements. They can be extended as well if need arises. In other words you are not always tied to a particular implementation. With Singleton you have the flexibility to make changes as when situation demands.
- Singleton Class can have value when Class object instantiated between server and client, such a way if three client want to have a shared data between them Singleton can be used. Static are always just shared and have no instance but multiple references.
- Singleton, as a non-static class, can still implement an interface, supports inheritance, etc. For example, you can’t inherit a static class and override a virtual method in it.
- Consider that today the application has a global shared object and tomorrow we decide that there has to be more than one instance of it. If you use a singleton, all you need to do is make your constructor public. But if you decide to use a static class, the change would have to be much more intrusive.
- If we ever decide to add unit tests to your code, replacing a singleton instance with a fake one is much easier (change in one place) compared to having to deal with a whole bunch of static functions that all share global data.
- If it is a Web application, Static Classes can create some major problems in a concurrent environment (because of shared data), specially when you are also using static variables and static methods.
- Singleton class does maintains state while Static doesn’t.
- We can pass Singleton object as parameter.
- If design calls for inheritance and variation then we must use a Singleton and not a Static class as we cannot use inheritance with static classes, by definition. In most usages, we would want our Singleton to implement an interface, something you cannot do with a static class.
- When we want to increase your performance we will encounter situations where we will have to use threading, so when making our application thread-safe we avoid some race situation.
- Static and Singleton classes will share there values for the entire instance of your application. When we access that object from multiple threads we can reuse or overwrite variables that’s also being used by another object. This will result in some very strange output and it’s hard sometimes to find what is going on since it ain’t easy to simulate this in test cases. We can implement a lock in your methods to introduce thread safety in these classes.