When writing an application a very common thing for a novice software developer to miss is Software Design Patterns. This article attempts to explain the Singleton Design Pattern. I am not going to provide any formal definition of it, I will just share what I understand about it. Let's consider a real-world example to understand it better.
Suppose your grandfather asks you to write a letter for him. The first thing you will do is buy a pen and a paper, you will tehn write it and show it to your grandfather. If he wants some changes tehn what will you do? Will you rush to the market again and buy a new pen to implement the changes or you will use the same pen? Well it is quite obvious that you will use the same old pen.
Now let's get a little technical, if you have created an object of a class once in your application then why not use the same object for that class everywhere, rather than flooding memory with unnecessary objects and this is what we call a Singleton Design Pattern, in other words a single object of each class.
Now the question is, how to do it. Time to code.
public class Singleton}
{
public void Print(string s)
{
Console.WriteLine(s);
}
}
I have declared a simple class here having one method Print. If we want to call the Print method from other classes then we can do that by creating the object of the singleton class using the "new" operator, right? I will implement logic that will prevent instantiation of the class using the "new" operator, if we not do this then developers will continue to create the objects. Confused? Don't be; just use a private constructor like this:
public class Singleton
{
private Singleton()
{
}
public void Print(string s)
{
Console.WriteLine(s);
}
}
Try to make the object of the class now using the "new" operator; it will throw a compile-time error. Until now our first objective has been completed. Let's move to the next step. We have prevented our class from beng instantiated with the "new" operator. It is therefore our responsibility to provide some other way for the developer to create its object and access its method. To do that I will declare a method GetObject() that will create the object and return it to the calling function and in this method I will check if an object of the class already exists; it it does then it will return the old object rather than creating a new one.
public class Singleton
{
protected static Singleton _obj;
private Singleton()
{
}
public static Singleton GetObject()
{
if (_obj == null)
{
_obj = new Singleton();
}
return _obj;
}
public void Print(string s)
{
Console.WriteLine(s);
}
}
You can see I have declared a static instance of the class _obj and in the GetObject() method I am checking if the instance is null; if it is then it will create a new instance and return it otherwise it will return the previously created object.
Use it in your main function like this:
class Program
{
static void Main(string[] args)
{
Singleton SingletonObject = Singleton.GetObject();
SingletonObject.Print("Hello World");
Console.ReadLine();
}
}
Complete Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Singleton SingletonObject = Singleton.GetObject();
SingletonObject.Print("Hello World");
Console.ReadLine();
}
}
public class Singleton
{
protected static Singleton _obj;
private Singleton()
{
}
public static Singleton GetObject()
{
if (_obj == null)
{
_obj = new Singleton();
}
return _obj;
}
public void Print(string s)
{
Console.WriteLine(s);
}
}
}