Facade Design Patter

This article provides a brief introduction to Facade Design Patterns.  Facade Design pattern provides an easy to use interface to an otherwise complicated collection of interfaces or subsystems.  It makes things easier by hiding the details of the implementation.

When designing good programs such as a recruitment website design, programmers usually attempt to avoid excess coupling between module/classes.  Using this pattern helps to simplify much of the interfacing that makes large amounts of coupling complex to use and difficult to understand.

In a nutshell, this is accomplished by creating a small collection of classes that have a single class (Facade) that is used to access them.

The facade pattern is an object-oriented design pattern.  A facade is an object that provides a simplified interface to a larger body of code, such as a class library.  A facade can accomplish all of the following.

  • It can make a software library easier to use and understand since the facade has convenient methods for common tasks.
  • It makes code that uses the library more readable for the same reason.
  • It can reduce dependencies of outside code on the inner workings of a library since most code uses the facade.  This allows for more flexibility in developing the system.
  • It can wrap a poorly designed collection of APIs with a single well-designed API.

Class Diagram

ClassDiagram2

Code in C#

This structural code demonstrates the Facade pattern which provides a simplified and uniform interface to a large subsystem of classes.

// Facade pattern -- Structural example  


using System;



namespace GangOfFour.Facade.Structural
{

    /// <summary>

    /// MainApp startup class for Structural

    /// Facade Design Pattern.

    /// </summary>

    class MainApp
    {

        /// <summary>

        /// Entry point into console application.

        /// </summary>

        public static void Main()
        {

            Facade facade = new Facade();



            facade.MethodA();

            facade.MethodB();



            // Wait for user

            Console.ReadKey();

        }

    }



    /// <summary>

    /// The 'Subsystem ClassA' class

    /// </summary>

    class SubSystemOne
    {

        public void MethodOne()
        {

            Console.WriteLine(" SubSystemOne Method");

        }

    }



    /// <summary>

    /// The 'Subsystem ClassB' class

    /// </summary>

    class SubSystemTwo
    {

        public void MethodTwo()
        {

            Console.WriteLine(" SubSystemTwo Method");

        }

    }



    /// <summary>

    /// The 'Subsystem ClassC' class

    /// </summary>

    class SubSystemThree
    {

        public void MethodThree()
        {

            Console.WriteLine(" SubSystemThree Method");

        }

    }



    /// <summary>

    /// The 'Subsystem ClassD' class

    /// </summary>

    class SubSystemFour
    {

        public void MethodFour()
        {

            Console.WriteLine(" SubSystemFour Method");

        }

    }



    /// <summary>

    /// The 'Facade' class

    /// </summary>

    class Facade
    {

        private SubSystemOne _one;

        private SubSystemTwo _two;

        private SubSystemThree _three;

        private SubSystemFour _four;



        public Facade()
        {

            _one = new SubSystemOne();

            _two = new SubSystemTwo();

            _three = new SubSystemThree();

            _four = new SubSystemFour();

        }



        public void MethodA()
        {

            Console.WriteLine("
MethodA() ---- ");

            _one.MethodOne();

            _two.MethodTwo();

            _four.MethodFour();

        }



        public void MethodB()
        {

            Console.WriteLine("
MethodB() ---- ");

            _two.MethodTwo();

            _three.MethodThree();

        }

    }

}

Conclusion

Thus, Facade is a design pattern that hides the details and complexities of the lower-level software services for which it is written, making the service easier to use.  In fact, the lower-level classes need not be classes at all; they can be an API in the form of a code library or a Web service.

A Facade also provides a unified entry point into the layers of the software.  This reduces the applicatio’s dependency on the software service details and allows the Facade to hide future changes in the software service itself.

References:

http://www.dofactory.com/patterns/patternfacade.aspx

http://aspalliance.com/970_Facade_Design_Pattern.4

Deepak Kamboj

Deepak Kamboj is a Solution Architect and Technology Enthusiast, located at Redmond, WA, having 14+ years of hands on experience in the IT industry.

You may also like...