Design Pattern Singleton design pattern

Design Pattern Singleton design pattern
Singleton Pattern:
It comes under the category of ‘Creational Pattern’.


It allows a single instance of a class and provides global access to it.

Steps to implement a Singleton Pattern in C#:
1. Create a sealed class.
2. Declare a private variable for that class inside the class.
3. Make the constructor as private.
4. Create a public static property which only returns the instance
5. Inside that use lock() to handle the object instance for supporting multi-threading functionality.

1. Logger
It is primarily used in the areas where single access channel is needed. Logging is an area where it is primarily used.
2. Db Connection
For managing database connections also it is used as there should be only one connection at a time.
3. Configuration Manager
Then configuration managers, which read and write to configuration files, there also Singletons are used.

A Logger example in C#:
Steps to write it:
1. Select Visual studio > Project > C# & Windows Application.
2. Follow the steps given in the steps to write a Singleton class above
3. The complete code of the Singleton Pattern and its usage class are given below :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SingletonDemo
public partial class Form1 : Form
public Form1()

private void Form1_Load(object sender, EventArgs e)
MySingleton sg = MySingleton.Instance;

public sealed class MySingleton
private static volatile MySingleton instSingleton;
private static object objSynformultithread = new Object();

private MySingleton()

public void LogMyError(String msg)
TextWriter twLog = new StreamWriter("c:\\log.txt");

// write a line of text to the file
twLog.WriteLine("Start" + DateTime.Now);
// close the stream

public static MySingleton Instance
if (instSingleton == null)
lock (objSynformultithread)
if (instSingleton == null)
instSingleton = new MySingleton();

return instSingleton;

Explanations over the Program:

Purpose of Sealed:
It prevents derivation which could add instances.

Purpose of Private Static Volatile:
Private, it makes the variable private and so not accessible from outside.
Static, without creating instance (only the instance created inside) we can call with name of the class as MySingleton.variable.
Volatile, makes the instance variable assignment completed before it is accessed.

Purpose of Lock:
It avoids deadlocks, enables multithreaded functionality.

The above program has the function LogMyError which does the logging to the file system. You can change the file name and path wherever you want it to get stored. Moreover it was written for a demo purpose only, so its not that much fuller. You need to add the append functionality to it such that it works as according to your need.

If you do the minor modifications based on your need of logging then it will be a good Logger for any kind of application.
Similarly it can be used for DB Connection, Configuration Management, etc.

No comments:

Top Blogs