Click here to Skip to main content
13,197,211 members (57,396 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 1 Dec 2010

A Pattern A Day Keeps Architect Away: Singleton

, 1 Dec 2010
Rate this:
Please Sign up or sign in to vote.
Article series on design patterns for Singleton


This is a short article series (articles being short and series being long) about design patterns. Design patterns are, as per my best understanding, a pair of problem and a solution for a given situation.

Let’s take an example from the computing world.

Problem and Situation: I want to search an array. It is given that the array is sorted.

Solution: Use Binary Search, because it will be faster.

Similarly a design pattern example could be...

Problem and Situation: I want only one instance of the given object in my system. This is required because the class represents my log file and will be accessed from multiple sources. Hence to avoid any race condition, I want to have only one instance of the class. In this case, because there exists an object, a static class will not make sense.

Solution: Use a singleton.

Why this Article Series will be Different

There are countless number of articles on the web on design patterns, so why would anyone want to read this? The reason is that in this series I will try to be more practical than abstract and it will be .NET C# driven hence even using language features to help implement the patterns. The treatment of the subject will be more examples oriented than abstract. Further, we will do what makes sense, if code is the best way to explain something then it's code, and if it needs complex state machines then it will be state machines.

Patterns are usually classified as design patterns and architecture patterns. Design patterns typically deal with problems localised into a subcomponent or a use case of the entire application, for example how do I implement my hierarchical help. Their application provides a readily available set of types which can be coded. Architecture patterns have a larger scope and provide guidelines for project. They end up providing the blue print against which a design will be done.

Design Pattern Types

According to the Gang of four book vis Design Patterns: Elements of Reusable Object-Oriented Software, patterns (GOF) may be classified into:

Creational Patterns – The creational patterns talk about how to create a new object, where do I write new in my code? Some of the magic they allow us to perform are having just one instance of a class using a singleton pattern; allow creating a heavy object using prototype and so on.

Structural Patterns – The structural patterns talk about how to create an object should I use inheritance, composition. In other words, how do I compose my elements to create a better class? Some of the magic they allow us to do is create an object whose hierarchy of classes and sub classes is dynamic using Decorator, creating a large number of similar objects using fly weight and so on.

Behavioural Patterns – These patterns deal with how objects communicate or in other words if I want to call a method of an object how do I do it. They allow neat magic like giving a class a new method without changing its code. Giving a general algorithm while requesting exact implementations for providing some specific logic for example sorting requires two loops but the < sign has to be provided by the exact implementation.

In this series, we will look into the GOF patterns and then other patterns which are not in GOF such as resurrection.

Pattern of the Day: Singleton

This is a creational pattern - it deals with how I create a new object. The Singleton pattern guarantees that only one instance of the class will be made available in the entire execution context or app domain.

Singleton is different from static where there is no instance.

How to do it without singleton?

Let's first try to create and maintain only one instance of the class without using Singleton.

public static MyClass myClass = null; 
public class MyClass() 
//Some logic 
public class MyUsageClass 
if(myClass == null) 
myClass = new MyClass(); 

There are a number of issues with this approach. For example, what if I need to maintain sync locks while creation of object, in that case the logic of this creation will be distributed all across the place. What if I want to run some special code before and after the instance is created and so on.

How To Do It In .NET


public class MyClass
private static MyClass myClass = null;

private MyClass()
//Any intialization code like open log file 

public static MyClass GetInstance()
if(MyClass.myClass == null)
MyClass.myClass = new MyClass();

return MyClass.myClass;

public static MyClass GetInstance
if (MyClass.myClass == null)
MyClass.myClass = new MyClass();

return MyClass.myClass;

The singleton way of doing things makes life a little simple. There exists a static instance of the class which is managed by the class itself.

To ensure no one can instance the class, we make the constructor private. There exists a method called GetInstance which the instance on the first use and returns the class. A similar job is done by the property GetInstance. Thus it is at designer's discussion to use one of the said techniques.

If it is required the constructor be thread safe, then code of the following structure may be used.

object locker = new object();
private MyClass()
lock (locker)
//Any initialization code like open log file 

The constructor is still responsible for creating a nice instance with all initialization done.

How Do I Pass Parameters?

I had been taking a session on singleton and someone asked me what if we need to pass parameters to the class during instantiation. If it occurs that you need to pass parameters to the instantiation logic, then this is not a case for singleton. This is because the class depends on some external conditions and hence a single instance will not suffice it’s working.

Any attributes required by a singleton should be made available using deployment time in a configuration.

Some Usage Scenarios of Singleton

Singletons can be used in a variety of scenarios, some of them include representing a physical resource like log files, network cards, and so on.


  • 2nd December, 2010: Initial post


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


About the Author

Architect Imfinity
India India
Hi I have been working on enterprise applications for last six years.

You may also be interested in...


Comments and Discussions

QuestionMy vote of 4 Pin
Peace ON20-Jul-13 1:08
memberPeace ON20-Jul-13 1:08 
Generalnice one Pin
Pranay Rana17-Jan-11 0:58
memberPranay Rana17-Jan-11 0:58 
GeneralMy vote of 3 Pin
CoolDadTx2-Dec-10 3:45
memberCoolDadTx2-Dec-10 3:45 
GeneralRe: My vote of 3 Pin
gaurav_verma_mca2-Dec-10 4:02
membergaurav_verma_mca2-Dec-10 4:02 
GeneralRe: My vote of 3 Pin
Frederic Sivignon2-Dec-10 5:14
memberFrederic Sivignon2-Dec-10 5:14 
GeneralRe: My vote of 3 Pin
gaurav_verma_mca2-Dec-10 17:52
membergaurav_verma_mca2-Dec-10 17:52 
GeneralRe: My vote of 3 Pin
PIEBALDconsult6-Dec-10 14:53
mvpPIEBALDconsult6-Dec-10 14:53 
GeneralRe: My vote of 3 Pin
gaurav_verma_mca6-Dec-10 16:37
membergaurav_verma_mca6-Dec-10 16:37 
GeneralRe: My vote of 3 Pin
PIEBALDconsult7-Dec-10 2:26
mvpPIEBALDconsult7-Dec-10 2:26 
GeneralRe: My vote of 3 Pin
gaurav_verma_mca7-Dec-10 5:50
membergaurav_verma_mca7-Dec-10 5:50 
GeneralRe: My vote of 3 Pin
PIEBALDconsult7-Dec-10 18:15
mvpPIEBALDconsult7-Dec-10 18:15 
GeneralRe: My vote of 3 Pin
gaurav_verma_mca8-Dec-10 22:42
membergaurav_verma_mca8-Dec-10 22:42 
GeneralToo Complex Pin
CoolDadTx2-Dec-10 3:45
memberCoolDadTx2-Dec-10 3:45 
GeneralRe: Too Complex Pin
gaurav_verma_mca2-Dec-10 4:01
membergaurav_verma_mca2-Dec-10 4:01 
GeneralRe: Too Complex Pin
xzz01952-Dec-10 7:02
memberxzz01952-Dec-10 7:02 
GeneralRe: Too Complex Pin
gaurav_verma_mca2-Dec-10 17:53
membergaurav_verma_mca2-Dec-10 17:53 
GeneralThoughts Pin
PIEBALDconsult2-Dec-10 2:27
mvpPIEBALDconsult2-Dec-10 2:27 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 2 Dec 2010
Article Copyright 2010 by gaurav_verma_mca
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid