Singleton Design Patterns in C#, Lazy and Eager Design Patterns

 

Design Pattern is nothing but a reusable solution for commonly occurring problems in Software design by using OOP’s concepts. 

In this article we discuss about Singleton design patterns in C#. 

Singleton design pattern is useful in most of the software applications design. By using Singleton design pattern we can maintain only one instance for a particular class through single access point. 

How to Create Singleton Design Pattern: 

To create the Singleton design pattern for specific class, we have to follow below rules. 

·         Class has to contain private constructor with no parameters to avoid the instantiating the class outside, because Singleton class itself has to return instance of the that class 

·         Class has to declare with the “sealed” keyword to avoid the inheritance 

·         Class has to declare with the “public” keyword for global access 

Here we discuss about Singleton design pattern in C# by taking company CEO information. Any company has only one CEO, so we will pull the CEO information with the help of Singleton design pattern as shown below.

 

namespace CSharpSingletonDesignPattern 

{ 

    ///<summary> 

    /// class declare with public and sealed keywords 

    /// sealed: to avoid inheritance from other class 

    /// public: for global access 

    ///</summary> 

    public sealed class CEO 

    { 

        private static CEO singleton;

 

        ///<summary> 

        /// private constructor to avoid the instantiating the class outside 

        ///</summary> 

        private CEO() { }

 

        public static CEO GetInstance() 

        { 

            if (singleton == null) 

            { 

                singleton = new CEO(); 

            } 

            return singleton; 

        }

    } 

}

 

using System; 

using System.Windows.Forms;

 

namespace CSharpSingletonDesignPattern 

{ 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        private void btn_Click(object sender, EventArgs e) 

        { 

            CEO obj1 = CEO.GetInstance(); 

            CEO obj2 = CEO.GetInstance();

 

            if (obj1 == obj2) 

            { 

                MessageBox.Show("Two instances are same, Singleton pattern is working fine"); 

            } 

        } 

    } 

} 

 

As shown above we have CEO class with required specifications for Singleton design pattern. CEO class has GetInstance() static method which returns instance of the CEO class. As shown above in Form1, we are calling the GetInstance() method two times in button click and comparing whether the two instances are same or not. The result is as shown below.

 

                              

 

As of now we discussed “lazy-initialize” a singleton in multi-threaded, means until unless we call the GetInstance() method the instance is not created for the class. 

Now we discussed the “eager-initialize” a singleton. Eager-initialize is same as lazy-initialize, but in eager-initialize instance of the class will create whenever the class is called (even though GetInstance() method not called) as shown below.   

 

namespace CSharpSingletonDesignPattern 

{ 

    ///<summary> 

    /// class declare with public and sealed keywords 

    /// sealed: to avoid inheritance from other class 

    /// public: for global access 

    ///</summary> 

    public sealed class CEO 

    { 

        #region Eager-intialized Singleton         

        private static readonly CEO singleton = new CEO();

 

        static CEO() 

        { 

        }

 

        private CEO() { }

 

        public static CEO GetInstance()

        { 

           return singleton; 

        } 

        #endregion 

    } 

}

 

As shown above instance is created for Singleton class whenever the class is called even though we are not calling the GetInstance() method. 

As of now we discussed about multi-threaded Singleton design pattern in C#. In my next article we discuss about Thread-safe Singleton desgin pattern in C#.

                                                                                                          CSharpSingletonDesignPattern.zip (42.85 kb)