Design Patterns – Facade

Facade is one of design patterns which are used most frequently. To explain what it is and how to use it, we can compare it to a black box. In black box test, tester is only allowed to create the input and get the output. He doesn’t know anything about processing inside of the system. You will meet similar situation in Facade – you don’t want to show out the whole functionality, you need to create some kind of interface which enables user to use the functionality, but he  cannot look into the system.

2 Steps to create a simple Facade:

1) Create classes with all functionality inside

2) Create a Facade class with instances of classes which had been prepared before and try to access

Sounds good? Let’s prepare a simple example (just to know how it works, in another example for this article I will show you how to prepare a well designed facade)

1) Open Visual Studio (in this example I used VS 2012 Ultimate)

2) Create a Console application project

3) Add Folder and name it „Facade”

4) Add ExternalCustomer.cs, InternalCustomer.cs and CustomerFacade.cs

InternalCustomer.cs and ExternalCustomer.cs are responsible for functionality, CustomerFacade.cs for user.

1

The ExternalCustomer.cs:

namespace Facade.Facade
{
    internal class ExternalCustomer
    {
        public string Introduce()
        {
            return "EXTERNAL CUSTOMER";
        }
    }
}

The InternalCustomer.cs:

namespace Facade.Facade
{
    internal class InternalCustomer
    {
        public string Introduce()
        {
            return "INTERNAL CUSTOMER";
        }
    }
}

Now the most important thing – our Facade:

namespace Facade.Facade
{
    internal class CustomerFacade
    {
        private ExternalCustomer externalCustomer;
        private InternalCustomer internalCustomer;

        public CustomerFacade(ExternalCustomer externalCustomer, InternalCustomer internalCustomer)
        {
            this.externalCustomer = externalCustomer;
            this.internalCustomer = internalCustomer;
        }

        public string IntroduceExternalCustomer()
        {
            return externalCustomer.Introduce();
        }

        public string IntroduceInternalCustomer()
        {
            return internalCustomer.Introduce();
        }
    }
}

In Main:

using System;

namespace Facade
{
    using global::Facade.Facade;

    class Program
    {
        static void Main(string[] args)
        {
            CustomerFacade customerFacade = new CustomerFacade(new ExternalCustomer(), new InternalCustomer());
            Console.WriteLine(customerFacade.IntroduceExternalCustomer());
            Console.WriteLine(customerFacade.IntroduceInternalCustomer());
            Console.ReadKey();
        }
    }
}

As you can see, in Main you operate only on your Facade methods. In the beginning of this post I have pointed that I would show the same, but well designed.

1) The first change is to add ICustomer interface which has definition of a method Introduce()

2) InternalCustomer.cs and ExternalCustomer.cs should implement the ICustomer interface

3) Another change is to add ICustomerFacade interface with 2 methods: IntroduceExternalCustomer() and IntroduceInternalCustomer()

4) CustomerFacade.cs should implement ICustomerFacade interface

The reason why you should keep code like this is to operate with interfaces.

Your code structure should look like below:

5

The ExternalCustomer.cs:

namespace PatternsExamples.Facade
{
    class ExternalCustomer : ICustomer
    {

        #region ICustomer Members

        public string Introduce()
        {
            return "EXTERNAL CUSTOMER";
        }

        #endregion
    }
}

The InternalCustomer.cs:

namespace PatternsExamples.Facade
{
    class InternalCustomer : ICustomer
    {
        #region ICustomer Members

        public string Introduce()
        {
            return "INTERNAL CUSTOMER";
        }

        #endregion
    }
}

Our CustomerFacade.cs:

namespace PatternsExamples.Facade
{
    internal class CustomerFacade : ICustomerFacade
    {

        private readonly ICustomer externalCustomer;

        private readonly ICustomer internalCustomer;

        public CustomerFacade(ICustomer exCustomer, ICustomer inCustomer)
        {
            this.externalCustomer = exCustomer;
            this.internalCustomer = inCustomer;
        }

        #region ICustomerFacade Members

        public string IntroduceExternalCustomer()
        {
            return externalCustomer.Introduce();
        }

        public string IntroduceInternalCustomer()
        {
            return internalCustomer.Introduce();
        }

        #endregion
    }
}

ICustomerFacade.cs:

namespace PatternsExamples.Facade
{
    interface ICustomerFacade
    {
        string IntroduceExternalCustomer();

        string IntroduceInternalCustomer();
    }
}

ICustomer.cs:

namespace PatternsExamples.Facade
{
    interface ICustomer
    {
        string Introduce();
    }
}

In Main:

using System;

namespace PatternsExamples
{
using PatternsExamples.Facade;

class Program
{
static void Main(string[] args)
{
ICustomerFacade customerFacade = new CustomerFacade(new ExternalCustomer(), new InternalCustomer());
Console.WriteLine(customerFacade.IntroduceExternalCustomer());
Console.WriteLine(customerFacade.IntroduceInternalCustomer());
Console.ReadKey();
}
}
}

That’s all!

3 uwagi do wpisu “Design Patterns – Facade

    1. Not exactly.

      Wrapper(Adapter) is used to adapt one interface to the another. For simple example – we have one system which needs to be connected with another. One is written in English, second is written in French – then we use adapter to convert interface of one class to another class. Summing up, adapter wraps old interface into the new one.

      Facade – we provide a universal interface to handle a group of interfaces. So we don’t provide a new interface instead of the old one. We use old interfaces with the new one.

      Hope everything is clear 🙂

      1. My advice is to not use name Wrapper at all. Use Facade instead. According to wiki these 3 are kind of same:
        -Wrapper,
        -Adapter,
        -Decorator.
        My advice is to distinct them according to the role they play in your code.
        Facades intent is to hide possible codebloat with few nice, public methods .
        Adapters intent is to adapt…
        Decorators intent is to extend original behavior with some extras.

Zostaw komentarz

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

w

Connecting to %s