Inversion of Control and Dependancy Injection

by Jacobus Meintjes June 17, 2009 16:13

Tags: ,

Design Patterns | IoC

Singleton Pattern

by Jacobus Meintjes April 06, 2009 13:39

Why? 

While on the subject of static classes I had a look at the Singleton pattern.  

The Singleton pattern is a creational pattern;  a creational pattern tries to remove a system from how it's objects are created. Other patterns in the creational patterns group is, Prototype pattern and Factory method. 

Definition

The Singleton pattern, is a design pattern that is used to restrict instantiation of a class to one object and provide a global access point(sourced from wikipedia). The object will be instantiated only once, and the object should only be created when required.

UML

Example Code

    //.Net Singleton

    public sealed class Singleton

    {

        //Creating an instance of the Singleton class only once.

        static readonly Singleton instance = new Singleton();

 

        //Private constructor

        private Singleton() { }

      

        public static Singleton Instance

        {

            get

            {

                return instance;

            }           

        }

    }

 

Or

 

    public class Singleton

    {

        // The combination of static and readonly makes the instantiation

        // thread safe.  Plus the constructor being protected (it can be

        // private as well), makes the class sure to not have any other

        // way to instantiate this class than using this member variable.

        public static readonly Singleton Instance = new Singleton();

 

        // private constructor is sufficient to avoid other instantiation

        // This must be present otherwise the compiler provides a default

        // public constructor

        //

        private Singleton()

        {

        }

    }

 

This code is thread-safe because the Framework ensures thread safety on static types that have been initialised.

Sample Singleton Code (from Microsoft)

 

    sealed class SingletonCounter

    {

        public static readonly SingletonCounter Instance =

             new SingletonCounter();

        private long Count = 0;

        private SingletonCounter() { }

        public long NextValue()

        {

            return ++Count;

        }

    }

 

    class SingletonClient

    {

        [STAThread]

        static void Main()

        {

            for (int i = 0; i < 20; i++)

            {

                Console.WriteLine("Next singleton value: {0}",

                    SingletonCounter.Instance.NextValue());

            }

        }

    }

 

The Singleton pattern is used in conjunction with many other patterns for example, the Builder, Prototype and Abstract Factory.

The effectiveness of the Singleton pattern relies entirely on developers all using the same rules. Even though implementing a singleton as outlined in this chapter doesn’t require much coding effort, it would be nice to be able to reuse the implementation. But if the standard rules are not followed, one developer may use an Instance property and another a Create method, for example, or one may use the nested class while the other doesn’t.

Tags:

Design Patterns

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant

About Me

Jacobus Meintjes
 
C# developer working with ASP.Net and/or Windows Forms.
Email Me

Cumulus

This will be shown to users with no Flash or Javascript.

Widget Twitter not found.

Root element is missing.X