Singleton Pattern

Most of you will be heard of this pattern. It is the simplest and popular pattern among the 23 design patterns.

Here we can examine the pattern using a Challenge / Solution scenario.  We will be using this type of approach in throughout the pattern learning.

Challenge

You are working on an application which requires Logging information into a file named Application.log. The logging is managed by a class named LogManager. The LogManager through its constructor keeps a lock on the File.

The LogManager instance an be created throughout the application and it will result in Exceptions. Above that there is only instance requirement for the LogManager class.

The above problem can be solved using Singleton Pattern.

Definition

“Ensure a class only has one instance, and provide a global point of access to it.”

Implementation

To implement the specification above we have to perform the following:

  • Make the Constructor Private and Create only 1 Instance
  • Create a Static Property to Access the Instance

Following is the modified class definition according to Singleton Pattern.

clip_image002

Following is the code contained in LogManager class.

public class LogManager

{

private static LogManager _instance;

public static LogManager Instance

{

get

{

if (_instance == null)

_instance = new LogManager();

return _instance;

}

}

private LogManager() // Constructor as Private

{

_fileStream = File.OpenWrite(GetExecutionFolder() + “\Application.log”);

_streamWriter = new StreamWriter(_fileStream);

}

private FileStream _fileStream;

private StreamWriter _streamWriter;

public void WriteLog(string message)

{

StringBuilder formattedMessage = new StringBuilder();

formattedMessage.AppendLine(“Date: ” + DateTime.Now.ToString());

formattedMessage.AppendLine(“Message: ” + message);

_streamWriter.WriteLine(formattedMessage.ToString());

_streamWriter.Flush();

}

public string GetExecutionFolder()

{

return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

}

}

You can see that the constructor is private here. As it is private outside classes cannot create instance of LogManager.

private LogManager() // Constructor as Private

{

}

You can see the new Instance property which is static. It also ensures only one instance is created.

public static LogManager Instance

{

get

{

if (_instance == null)

_instance = new LogManager();

return _instance;

}

}

For invoking the write method we can use the following code:

LogManager.Instance.WriteLog(“Test Writing”);

Following image depicts the Number of Instances in Singleton and Normal Scenario.

clip_image004

The advantage of using Singleton is listed below:

  • Instance Reuse makes Effective Memory Management
  • File Locking Overheads are Avoided

The limitations of Singleton are listed below:

  • Making Constructor as Private impose Restrictions
  • More Overheads in Multi-threaded usage (But Thread Safe in .Net)

Summary

In this article we have seen what is Singleton pattern and a possible implementation of it using C#.NET. Multiton is another pattern which is often compared with Singleton.

4 thoughts on “Singleton Pattern

  1. Prometheus says:

    What is the difference if I use simple static class here. I mean instead of using private constructor it will be static constructor…

    1. Jean Paul says:

      Good Question!
      I had a Good Answer! (probably)

      Singleton makes the methods/property as instance methods
      So you can pass the object instance across other methods. In static class this will be hinder as class reference should be there.

      Singleton do not restricts on inheritance chain as do static class

      Singleton provides the same interface reference flexibilities.
      eg: c1 : i1
      you can pass c1.Instance to i1 types… (so loosely coupled advantages are still maintained)

      Please let me know if you need a further clarification!

  2. Steve Min says:

    Why is Singleton Thread Safe in .Net?
    I think The Below Code is Thread safe in .Net:
    public class LogManage
    {
    public static readonly LogManage Instance=new LogManage();
    private LogManage(){…}
    }

    1. jeanpaulva says:

      Thread Safety is an Internal Infrastructure advantage provided by .Net.

      My experiments with static classes with multiple threads, proven that they are thread-safe :)

      JP

Leave a Reply to jeanpaulva Cancel reply

Your email address will not be published. Required fields are marked *