locked
Singleton Pattern Example RRS feed

  • Question

  • User2017673925 posted

    I am working on singleton desing patterns and want some real world example of design patterns.

    Can you please give me an example how you have used singleton pattern in you project.  I would appreciate if you can provide code.

    Thanks.

    Wednesday, June 16, 2010 3:43 PM

Answers

  • User-457353196 posted

    Here is an example - Its simple thread safety SingleTon pattern

    public sealed class Singleton
    {
        static Singleton instance=null;
        static readonly object padlock = new object();

        Singleton()
        {
        }

        public static Singleton Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance==null)
                    {
                        instance = new Singleton();
                    }
                    return instance;
                }
            }
        }
    }

     

    Reference URL - http://www.yoda.arachsys.com/csharp/singleton.html

    Shiva Mamidi

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 17, 2010 8:51 AM

All replies

  • User541108374 posted

    Hi,

    you can take a look here http://www.dofactory.com/Patterns/PatternSingleton.aspx for more information and working sample code.

    Grz, Kris.

    Wednesday, June 16, 2010 4:35 PM
  • User-457353196 posted

    Here is an example - Its simple thread safety SingleTon pattern

    public sealed class Singleton
    {
        static Singleton instance=null;
        static readonly object padlock = new object();

        Singleton()
        {
        }

        public static Singleton Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance==null)
                    {
                        instance = new Singleton();
                    }
                    return instance;
                }
            }
        }
    }

     

    Reference URL - http://www.yoda.arachsys.com/csharp/singleton.html

    Shiva Mamidi

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 17, 2010 8:51 AM
  • User-2004844803 posted

    Here is an example - Its simple thread safety SingleTon pattern

    public sealed class Singleton
    {
        static Singleton instance=null;
        static readonly object padlock = new object();

        Singleton()
        {
        }

        public static Singleton Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance==null)
                    {
                        instance = new Singleton();
                    }
                    return instance;
                }
            }
        }
    }


    Reference URL - http://www.yoda.arachsys.com/csharp/singleton.html

    Shiva Mamidi


    PS // Don't return the instance inside the lock, return the instance after you have unlocked padlock.

    Thursday, June 17, 2010 9:00 AM