Singleton Design Pattern

-> Only one instance of object is created.
-> is a creational design pattern which makes sure that you have one single instance of a particular class in the duration of your runtime, and provides a global point of access to the single instance.

-> The main disadvantage of this implementation is that it is not safe for multithreaded environments. If separate threads of execution enter the Instance property method at the same time, more than one instance of the Singleton object may be created.

-> The above approach ensures that only one instance is created and only when the instance is needed.
Also variable is declared to be volatile to ensure that assignment to the instance variable completes before the instance variable can be accessed. Lastly this approach uses a syncRoot instance to lock on, rather than locking on the type itself, to avoid deadlocks.

Leave a Reply

Your email address will not be published. Required fields are marked *