Creational Pattern, Singleton Design Pattern, Example of Singleton Pattern in C#

It is a creational pattern. Creational patterns are the patterns which separate a system from how its objects are created, composed and represented. Creational pattern hides the details of how the instances of the class are created.

 

Singleton Patterns in C#:

The purpose of singleton pattern is restricting the number of instances to one only one instance and making availability of that instance globally, it is a global access point further whenever need to create instance for particular class.

 

In singleton Design pattern, object of particular class is created with in that class and not allowed to create any object outside of that class. Within the singleton class also the object is not created until it is actually required, that means instance is created whenever it receives request.

 

In singleton design patterns, singleton class itself responsible to create the object and maintaining the object. Client not allowed creating any object for that singleton class.

 

Every singleton class needs to have private constructor to avoid the creating of object outside of the class, private readonly object which internally creates the instance and a public property or public method which is global access point to access the unique instance which is created internally. The singleton class should be sealed to avoid inherit from some other class.

 

The singleton pattern example looks like below.

 

Public sealed class ClassName

{

            //create private constructor

//readonly object to create the instance of ClassName class

            //public method or property to make the availability of internally created object for outside

}

 

 

Advantages of singleton design pattern:

If you want to maintain the instance of any class as unique, you can use singleton pattern in c#.

 

If you want to control the instance of any class at one place you can use singleton pattern in .net.

 

If you want to make available the instance of any class globally and unique, you can use singleton design patterns in c#.

 

Here we will discuss singleton design pattern with an example.

 

Example of Singleton Pattern in C#:

For example, you are displaying the users in your admin panel, several places from XML file. So you are reading the information from XML file at one place and making global access, because of this you can easily control the object creation and reading data from source.

 

Below is the sample class SingletonPattern for singleton design pattern.

 

public sealed class SingletonPattern

{

        //private constructor to avoid creating the object from outside of the class

        private SingletonPattern()

        {

            GetUserInfo();

        }

 

        //private readonly object which creates object internally

        private static readonly SingletonPattern instance = new SingletonPattern();

 

        //public property which provides access to unique instance globally

        public static SingletonPattern GlobalInstance

        {

            get

            {

                return instance;

            }

        }

 

        private DataSet _userData = new DataSet();

        public DataSet UserData

        {

            get { return _userData; }

        }

 

        private void GetUserInfo()

        {      

              _userData.ReadXml("../../Users.xml");

        }

 }

 

Here you are restricting the class from inheritance by using sealed keyword, restricting creation of object from outside by creating private constructor. You are creating private readonly instance which is internally creating the object as shown below.

 

 

        //private readonly object which creates object internally

        private static readonly SingletonPattern instance = new SingletonPattern();

 

 

And make this object global by creating the public property as shown below.

 

//public property which provides access to unique instance globally

 

public static SingletonPattern GlobalInstance

{

        get

        {

            return instance;

        }

}

 

Calling the GetUserInfo() method in constructor which loads user data into dataset.

We are calling this class in windows form load as shown below.

 

public partial class Form1 : Form

{

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            //calling singleton design pattern global access point which returns unique instance

            SingletonPattern objSingleton = SingletonPattern.GlobalInstance;

            dataGridView1.DataSource = objSingleton.UserData.Tables[0];

        }

}

 

Call the public property GlobalInstance of SingletonPattern class which returns unique instance of SingletonPattern class. By using this instance we are loading the user information into datagridview through SingletonPattern property UserData.

                                                         

                                                                                              SingletonPatternExample.zip (40.77 kb)