Design patterns in C# – part 7 (Facade pattern)

Role

The role of the Facade pattern is to provide different high-level views of subsystems whose details are hidden from users.

Design

FacadePattern

Implementation

  • Client -> TestFacadePattern
  • Facade -> Facade
  • Subsystems -> SystemOne, SystemTwo, SystemThree
namespace FacadePattern
{
    public interface IFacade
    {
        void OperationA();
        void OperationB();
        void OperationC();
    }

    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
    }

    internal class SystemThree : ISystem
    {
        #region ISystem Members

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

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

        #endregion
    }
}
using System;

namespace FacadePattern
{
    public class TestFacadePattern
    {
        public static void Main(string[] args)
        {
            IFacade facade = new Facade();
            Console.WriteLine("Operation A of facade: ");
            facade.OperationA();
            Console.WriteLine("Operation B of facade: ");
            facade.OperationB();
            Console.WriteLine("Operation C of facade: ");
            facade.OperationC();
        }
    }

    public class Facade : IFacade
    {
        private readonly SystemOne _systemOne;
        private readonly SystemThree _systemThree;
        private readonly SystemTwo _systemTwo;

        public Facade()
        {
            this._systemOne = new SystemOne();
            this._systemTwo = new SystemTwo();
            this._systemThree = new SystemThree();
        }

        #region IFacade Members

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

        public void OperationB()
        {
            this._systemTwo.A();
            this._systemThree.B();
        }

        public void OperationC()
        {
            this._systemThree.A();
            this._systemOne.B();
        }

        #endregion
    }
}

OUTPUT:

Operation A of facade:
Operation A in SystemOne !
Operation B in SystemTwo !
Operation B of facade:
Operation A in SystemTwo !
Operation B in SystemThree !
Operation C of facade:
Operation A in SystemThree !
Operation B in SystemOne !

Use when

A system has several identifiable subsystems and:

  • The abstractions and implementations of a subsystem are tightly coupled.
  • The system evolves and gets more complex but early adopters might want to retain their simple views.
  • You want to provide alternative novice, intermediate, and “power user” interface.
  • There is a need for an entry point to each level of layered software.

But consider using instead:

  • The Abstract Factory pattern for designs where subsystems create objects on behalf of the client.

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