Click here to Skip to main content
Click here to Skip to main content

Singleton Pattern – Positive and Negative Aspects

, 28 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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.

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.

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.

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

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)

Share

About the Author

John T.Emmatty
Software Developer (Senior) WEPA
India India
My aim is to write articles in plain English without any convoluted or periphrastic statements.
Follow on   Twitter

Comments and Discussions

 
QuestionSingleton Pattern Example PinmemberDeveloper Hemu Patel8-Aug-14 4:13 
QuestionVery nice post. Need more details on dependency injection PinmemberMember 41554066-Aug-14 19:34 
QuestionExcellent! PinmemberMember 1095199816-Jul-14 18:04 
QuestionExcellent article ! PinmemberBillWoodruff23-Oct-13 1:49 
GeneralMy vote of 5 PinmemberPeace ON20-Jul-13 0:38 
GeneralStatic classes [modified] PinmemberIan A Davidson10-Apr-13 16:23 
GeneralMy vote of 5 PinmemberAkhil_Mittal26-Feb-13 23:23 
QuestionSingleton Pattern Pinmemberpanqnik20-Nov-12 0:32 
Suggestiongive real life example Pinmembervithal wadje13-Sep-12 21:19 
GeneralMy vote of 4 PinmemberItz.Irshad5-Sep-12 1:27 
QuestionSingleton inheritance [modified] PinmemberAndyI3-Jan-12 8:16 
AnswerRe: Singleton inheritance PinmemberIan A Davidson10-Apr-13 16:56 
GeneralSingleton with subclassing PinmemberAbey Thomas2-Jan-12 17:46 
GeneralRe: Singleton with subclassing PinmemberJohn T.Emmatty3-Jan-12 5:08 
QuestionSingleton Pinmembergeoyar2-Jan-12 15:45 
AnswerRe: Singleton PinmemberJohn T.Emmatty3-Jan-12 5:13 
GeneralRe: Singleton Pinmembergeoyar4-Jan-12 14:28 
GeneralRe: Singleton PinmemberIan A Davidson10-Apr-13 17:10 
AnswerRe: Singleton PinmemberPaul893-Jan-12 6:42 
GeneralMy vote of 5 PinmemberHamidYaseen30-Dec-11 4:39 
GeneralRe: My vote of 5 PinmemberJohn T.Emmatty3-Jan-12 5:01 
GeneralMy vote of 5 Pinmembermaq_rohit29-Dec-11 2:24 
GeneralRe: My vote of 5 PinmemberJohn T.Emmatty3-Jan-12 1:17 
QuestionMemoryBarrier() ? PinmemberAxel Rietschin28-Dec-11 22:33 
AnswerRe: MemoryBarrier() ? PinmemberJohn T.Emmatty3-Jan-12 1:17 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 28 Dec 2011
Article Copyright 2011 by John T.Emmatty
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid