Singleton Design Pattern
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class Singleton
{
// lazy initialization - only object is created first and reference is created when ever required.
protected static Singleton testobj;
// Default constructor is made private inorder to stop creating instances for the Singleton class.
private Singleton()
{
}
// this method will check if the object of the class already exists else it will create and returns it
public static Singleton GetObjectReference()
{
if (testobj == null)
{
testobj = new Singleton();
}
return testobj;
}
public void Print(string s)
{
Console.WriteLine(s);
}
}
}
The singleton pattern is a design pattern that restricts the instantiation of a class to one object.
ReplyDelete