Jan 022013
 

Facade is a structural design pattern. It defines a higher-level interface that makes the subsystem easier to use. 

Facade means the mask (मुखौटा) and the pattern is nothing but a simplified interface to a more complex system by providing  a wrapper for the sub-system.

Consider starting your car, let us see what happens under the hood when you turn the key (or press button) to start your car:

car_engine

  1. First the starter motor comes to life,
  2. then the engine begins to turn and the spark plugs fire.
  3. When the key is turned, the starter motor “turns the engine over,” which means it turns the crankshaft.
  4. Crankshaft, in turn gets the pistons moving in the cylinders.
  5. This starts the engine cycle.
  6. Air and fuel is drawn into the cylinders, it is compressed and the then the spark plugs fire.
  7. This begins combustion.

And your car is now Start. To do all this there are 3 sub-systems:

  1. Battery (marked as ‘1’ in the diagram).
  2. Charging System (marked ‘2’ in the diagram).
  3. Starting system (marked ‘3’ in the diagram).

Imagine if there is no key and you have to start the engine yourself by doing  all the above tasks.. It will be cumbersome.

The key is a FACADE which hides this complicated system of Car-Starting. The below class diagram is taken from the Facade page of wiki:

Facade design pattern

In the above diagram, if there is no Facade, then the doSomething function must be implemented in both Client1 & Client2. Hence, the code repetition will be there. Facade design pattern reduces the code repetition. The elements of diagram are as below:

  • Facade: This class contains the set of simple functions that are made available to its users (APIs) and that hide the complexities of the difficult-to-use subsystem.
  • PackageN: The complex subsystem.
  • ClientN: The classes that want to access the subsystem.

Another example of Facade is the customer service station.

facade

The consumer calls one number and speaks with a customer service representative. The customer service representative acts as a Facade, providing an interface to multiple divisions.

Essentially in Facade, we are designing a wrapper class to encapsulate the functionality. It captures the collaboration of components and provides a simpler interface to the client.

Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.

Example Code:

/** Facade Design Pattern.
 */

/** The 'Subsystem ClassA' class
 */
class SubSystemOne
{
  public: 
    void MethodOne()
    {
      cout<< " SubSystemOne Method";
    }
};

/** The 'Subsystem ClassB' class
 */
class SubSystemTwo
{
  public:
    void MethodTwo()
    {
      cout<<" SubSystemTwo Method";
    }
};

/** The 'Subsystem ClassC' class
 */
class SubSystemThree
{
  public:
    void MethodThree()
    {
      cout<<" SubSystemThree Method";
    }
};

/** The 'Facade' class (Wrapper)
 */
class Facade
{
  private:
    SubSystemOne _one;
    SubSystemTwo _two;
    SubSystemThree _three;

  public:
    // Constructor 
    Facade();

    void MethodA()
    {
      cout<< "\nMethodA() ---- ";
      _one.MethodOne();
      _two.MethodTwo();
    }

    void MethodB()
    {
      cout<<"\nMethodB() ---- ";
      _two.MethodTwo();
      _three.MethodThree();
    }
}; 

int Main()
{
    Facade facade;

    facade.MethodA();
    facade.MethodB();

    // Wait for user
    getchar();
}

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)