Click here to Skip to main content
15,881,089 members
Articles / All Topics

Singleton Pattern – Positive and Negative Aspects

Rate me:
Please Sign up or sign in to vote.
4.76/5 (38 votes)
28 Dec 2011CPOL5 min read 179.6K   48   24
This article describes the pros and cons of the Singleton pattern.

Introduction

The Singleton pattern is probably the most famous and at the same time the most controversial pattern known to us. It must be also be the simplest pattern to learn and implement. Like any other pattern, Singleton exists to solve a common business problem that is ‘managing the state of a resource’. But does it solve the real problem or introduce additional problems? That is exactly the topic I am covering here.

Positive Sides of Singleton

One of the toughest issues to debug is the one created by the multiple instances of a class which manages the state of a single resource. It is highly desirable if we can use some Design Pattern to control the access to that shared resource. The Singleton pattern fits the bill perfectly to solve this scenario; by wrapping a singleton class around this problem ensures that there will be only one instance of the class at any given time. A most common and clichéd example for a singleton class is the one used for logging purposes where the whole application needs only one logger instance at anytime.

The anatomy of a singleton class is very simple to understand. The class typically has a private constructor which will prohibit you to make any instance of the singleton class; instead you will access a static property or static function of the singleton class to get the reference of a preconfigured instance. These properties/methods ensure that there will be only one instance of the singleton class throughout the lifetime of the application.

The one and only instance of a singleton class is created within the singleton class and its reference is consumed by the callers. The creation process of the instance can be done using any of the following methods:

1. Lazy Instantiation

If you opt for the lazy instantiation paradigm, then the singleton variable will not get memory until the property or function designated to return the reference is first called. This type of instantiation is very helpful if your singleton class is resource intense.

Image 1

However, the above implementation is not taking any precautions to be thread safe. That is, there may be situations like two or more threads accessing the Instance property at the same time which will create more than one instance of the singleton class.

We can use various thread synchronization techniques to combat the circumstances said above. One way is the use of double-checked locking. In double-checked locking, synchronization is only effective when the singleton variable is null, i.e., only for the first time call to Instance. This helps us to limit the performance penalty that comes along with the synchronization object to only happen once.

Image 2

2. Static Initialization

In static initialization, memory is allocated to the variable at the time it is declared. The instance creation takes place behind the scenes when any of the member singleton classes is accessed for the first time. The main advantage of this type of implementation is that the CLR automatically takes care of race conditions I explained in lazy instantiation. We don't have to use any special synchronization constructs here. There are no significant code changes in the singleton implementation when you switch from lazy instantiation to static initialization. The only change is that the object creation part is moved to the place where we are declaring the variable.

Image 3

Inheritance of Singleton Class

Inheriting a singleton class should be prohibited. Making a singleton class inheritable means any number of child classes can inherit from it creating multiple instances of the singleton class which will obviously violate the principle of singletons.

Singleton Class vs. Static Methods

Singleton takes over static classes on the following shortcomings:

  1. Static classes don’t promote inheritance. If your class has some interface to derive from, static classes makes it impossible.
  2. You cannot specify any creation logic with static methods.
  3. Static methods are procedural code.

Negative Sides of Singleton

The following points are used against the Singleton pattern:

  1. They deviate from the Single Responsibility Principle. A singleton class has the responsibility to create an instance of itself along with other business responsibilities. However, this issue can be solved by delegating the creation part to a factory object.
  2. Singleton classes cannot be sub classed.
  3. Singletons can hide dependencies. One of the features of an efficient system architecture is minimizing dependencies between classes. This will in turn help you while conducting unit tests and while isolating any part of the program to a separate assembly. A singleton will make you sacrifice this feature in your application. Since the object creation part is invisible to us, we cannot expect the singleton constructor to accept any parameters. This setback may look unimportant on the first glance but as the software complexity increases, it will limit the flexibility of the program.

Programmers often resort to the idea of Dependency Injection to overcome this problem. When dependency Injection is used, Singleton instance is not retrieved inside the class but is passed through the constructor or a property

C#
IMathFace obj = Singleton.Instance;
SingletonConsumer singConsumer = new SingletonConsumer(obj);
singConsumer.ConsumerAdd(10,20);

In the above example, SingletonConsumer's constructor accepts a parameter of type IMathFace. It can be the real singleton instance or a mock object.

When to Use a Singleton Class?

There is no straightforward answer to this question. A scenario which is acceptable to some will be unacceptable to others.

However, it is commonly accepted that the singleton can yield best results in a situation where various parts of an application concurrently try to access a shared resource. An example of a shared resource would be Logger, Print Spooler, etc.

The following points are suggested to be considered while designing a singleton class:

  1. Singleton classes must be memory-leak free. The instance of the singleton class is to be created once and it remains for the lifetime of the application.
  2. A real singleton class is not easily extensible.
  3. Derive the singleton class from an interface. This helps while doing unit testing (using Dependency Injection).

Conclusion

The notion of Singleton pattern replacing global variables is not always true. You should keep all the positive and negative aspects of the Singleton pattern in mind before deciding on it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) WEPA
India India
My aim is to write articles in plain English without any convoluted or periphrastic statements.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Archana Suresh 20225-May-22 23:50
Archana Suresh 20225-May-22 23:50 
QuestionPerformance of Singleton Pin
alpesh choubisa25-Dec-18 8:47
alpesh choubisa25-Dec-18 8:47 
QuestionVery nice post. Need more details on dependency injection Pin
Member 41554066-Aug-14 19:34
Member 41554066-Aug-14 19:34 
QuestionExcellent! Pin
Member 1095199816-Jul-14 18:04
Member 1095199816-Jul-14 18:04 
QuestionExcellent article ! Pin
BillWoodruff23-Oct-13 1:49
professionalBillWoodruff23-Oct-13 1:49 
GeneralStatic classes Pin
Ian A Davidson10-Apr-13 16:23
Ian A Davidson10-Apr-13 16:23 
GeneralMy vote of 5 Pin
Akhil Mittal26-Feb-13 23:23
professionalAkhil Mittal26-Feb-13 23:23 
Suggestiongive real life example Pin
vithal wadje13-Sep-12 21:19
vithal wadje13-Sep-12 21:19 
GeneralMy vote of 4 Pin
Itz.Irshad5-Sep-12 1:27
Itz.Irshad5-Sep-12 1:27 
QuestionSingleton inheritance Pin
AndyI3-Jan-12 8:16
AndyI3-Jan-12 8:16 
AnswerRe: Singleton inheritance Pin
Ian A Davidson10-Apr-13 16:56
Ian A Davidson10-Apr-13 16:56 
GeneralSingleton with subclassing Pin
Abey Thomas2-Jan-12 17:46
Abey Thomas2-Jan-12 17:46 
GeneralRe: Singleton with subclassing Pin
John T.Emmatty3-Jan-12 5:08
John T.Emmatty3-Jan-12 5:08 
QuestionSingleton Pin
geoyar2-Jan-12 15:45
professionalgeoyar2-Jan-12 15:45 
AnswerRe: Singleton Pin
John T.Emmatty3-Jan-12 5:13
John T.Emmatty3-Jan-12 5:13 
GeneralRe: Singleton Pin
geoyar4-Jan-12 14:28
professionalgeoyar4-Jan-12 14:28 
GeneralRe: Singleton Pin
Ian A Davidson10-Apr-13 17:10
Ian A Davidson10-Apr-13 17:10 
AnswerRe: Singleton Pin
pbalaga3-Jan-12 6:42
pbalaga3-Jan-12 6:42 
GeneralMy vote of 5 Pin
HamidYaseen30-Dec-11 4:39
professionalHamidYaseen30-Dec-11 4:39 
GeneralRe: My vote of 5 Pin
John T.Emmatty3-Jan-12 5:01
John T.Emmatty3-Jan-12 5:01 
GeneralMy vote of 5 Pin
maq_rohit29-Dec-11 2:24
professionalmaq_rohit29-Dec-11 2:24 
GeneralRe: My vote of 5 Pin
John T.Emmatty3-Jan-12 1:17
John T.Emmatty3-Jan-12 1:17 
QuestionMemoryBarrier() ? Pin
Axel Rietschin28-Dec-11 22:33
professionalAxel Rietschin28-Dec-11 22:33 
AnswerRe: MemoryBarrier() ? Pin
John T.Emmatty3-Jan-12 1:17
John T.Emmatty3-Jan-12 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.