A day with .Net

My day to day experince in .net

Singleton – Normal-Thread Safe-Lazy

Posted by vivekcek on September 8, 2012

Ensure that only one instance of a class is created.
Provide a global point of access to the object

Not Thread Safe Implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Singleton
{
    class Program
    {
        static void Main(string[] args)
        {
           Console.WriteLine(Singleton.Instance.DoSomething("Hay"));
           Console.WriteLine(Singleton.Instance.DoSomething("Halo"));
           Console.ReadLine();
        }
    }

    /// <summary>
    /// Not thread safe single implementation
    /// </summary>
    public class Singleton
    {
        /// <summary>
        /// Private variable hold the instance.
        /// </summary>
        private static Singleton _Instance = null;

        /// <summary>
        /// Private constructor to prevent direct instance creation.
        /// </summary>
        private Singleton() { }

        /// <summary>
        /// The only entry point to get an instance of the class.
        /// </summary>
        public static Singleton Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new Singleton();
                    return _Instance;
                }
                else
                    return _Instance;
            }
        }

        /// <summary>
        /// Some Functionality.Hash code indicate same instance.
        /// </summary>
        /// <returns></returns>
        public string DoSomething(string param)
        {
            return param + "from " + _Instance.GetHashCode();
        }


    }
}

Thread Safety By Double Lock

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Singleton
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Singleton.Instance.DoSomething("Hay"));
            Console.WriteLine(Singleton.Instance.DoSomething("Halo"));
            Console.ReadLine();
        }
    }

    /// <summary>
    /// Not thread safe single implementation
    /// </summary>
    public class Singleton
    {
        private static readonly object _SyncLock = new object();
        /// <summary>
        /// Private variable hold the instance.
        /// </summary>
        private static Singleton _Instance = null;

        /// <summary>
        /// Private constructor to prevent direct instance creation.
        /// </summary>
        private Singleton() { }

        /// <summary>
        /// The only entry point to get an instance of the class.
        /// </summary>
        public static Singleton Instance
        {
            get
            {

                if (_Instance == null)
                {
                    lock (_SyncLock)
                    {
                        if (_Instance == null) //Double check
                            _Instance = new Singleton();
                    }

                }
                return _Instance;
            }
        }

        /// <summary>
        /// Some Functionality.Hash code indicate same instance.
        /// </summary>
        /// <returns></returns>
        public string DoSomething(string param)
        {
            return param + "from " + _Instance.GetHashCode();
        }


    }
}

Singleton lazy instantiation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Singleton
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Singleton.Instance.DoSomething("Hay"));
            Console.WriteLine(Singleton.Instance.DoSomething("Halo"));
            Console.ReadLine();
        }
    }

    /// <summary>
    /// Not thread safe single implementation
    /// </summary>
    public class Singleton
    {
                
        /// <summary>
        /// Private constructor to prevent direct instance creation.
        /// </summary>
        private Singleton() { }

        /// <summary>
        /// The only entry point to get an instance of the class.
        /// </summary>
        public static Singleton Instance
        {
            get { return Nested.Instance; }
        }

        /// <summary>
        /// Some Functionality.
        /// </summary>
        /// <returns></returns>
        public string DoSomething(string param)
        {
            return param ;
        }

        /// <summary>
        /// Nested class for lazy
        /// </summary>
        private class Nested
        {
            static Nested()
            { }
            internal static readonly Singleton Instance = new Singleton();
        }


    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s