Design patterns in C# – part 10 (Singleton pattern)

Role

The purpose of the Singleton pattern is to endure that there is only one instance of a class, and that there is a global access point to that object.

Design

SingletonPattern

Implementation

  • Singleton -> Facade
namespace FacadePattern
{
    public interface IFacade
    {
        void OperationFirst();
        void OperationSecond();
        void OperationThird();
    }

    public interface ISystem
    {
        void A();
        void B();
    }
}
using System;

namespace FacadePattern
{
    internal class SystemOne : ISystem
    {
        #region ISystem Members

        public void A()
        {
            Console.WriteLine("Operation A in SystemOne !");
        }

        public void B()
        {
            Console.WriteLine("Operation B in SystemOne !");
        }

        #endregion
    }

    internal class SystemTwo : ISystem
    {
        #region ISystem Members

        public void A()
        {
            Console.WriteLine("Operation A in SystemTwo !");
        }

        public void B()
        {
            Console.WriteLine("Operation B in SystemTwo !");
        }

        #endregion
    }
}
namespace FacadePattern
{
    public class Facade : IFacade
    {
        private readonly SystemOne _systemOne;
        private readonly SystemTwo _systemTwo;

        private Facade()
        {
            this._systemOne = new SystemOne();
            this._systemTwo = new SystemTwo();
        }

        public static Facade Instance
        {
            get { return SingletonCreator.Instance; }
        }

        #region IFacade Members

        public void OperationFirst()
        {
            this._systemOne.A();
            this._systemTwo.B();
        }

        public void OperationSecond()
        {
            this._systemTwo.A();
        }

        public void OperationThird()
        {
            this._systemOne.B();
        }

        #endregion

        #region Nested type: SingletonCreator

        private class SingletonCreator
        {
            static SingletonCreator() {}
            internal static readonly Facade Instance = new Facade();
        }

        #endregion
    }
}
using System;

namespace FacadePattern
{
    public class TestFacadePattern
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Operation first of facade singleton: ");
            Facade.Instance.OperationFirst();
            Console.WriteLine("Operation second of facade singleton: ");
            Facade.Instance.OperationSecond();
            Console.WriteLine("Operation third of facade singleton: ");
            Facade.Instance.OperationThird();
        }
    }
}

OUTPUT:

Operation first of facade singleton:
Operation A in SystemOne !
Operation B in SystemTwo !
Operation second of facade singleton:
Operation A in SystemTwo !
Operation third of facade singleton:
Operation B in SystemOne !

Use when

  • You need to ensure there is only one instance of a class.
  • Controlled access to that instance is essential.
  • You might need more than one instance at a later stage.
  • The control should be localized in the instantiated class, not in some other mechanism.

Bibliography

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