what is the advantage of Singleton Design Pattern

To assure only one and same instance of object every time.

Take a scenario, say for a Company application, there is only one CEO. If you want to create or access CEO object, you should return the same CEO object every time.

One more, after logging into an application, current user must return same object every time.


Benefits of Singleton Pattern:

• Instance control: Singleton prevents other objects from instantiating their own copies of the Singleton object, ensuring that all objects access the single instance.

• Flexibility: Since the class controls the instantiation process, the class has the flexibility to change the instantiation process.

The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.


Real time usages/benefits of Singleton Design Pattern.

  1. While using multi-threading, to manage the multi-thread Pool.
  2. to manage the "service host repositories" in SOA (service oriented architecture).
  3. for Logging Framework implementation
  4. in automation Testing/Unit Testing project i.e. Coded UI projects.
  5. While implementing the Caching in a big application.
  6. for configuration settings to make proper control over the application.

Other answers are good, as well. But they are providing examples of behavioural characteristics of the pattern. But, Singleton is more about creation. Thus one of the most important benefit of the pattern is that it is resource friendly. You are not wasting memory for a new object when you actually do not need a new one.

This causes another benefit, which is the instantiation overhead is avoided.

Tags:

C#

Oop