Posted by : Sushanth Wednesday 16 December 2015

Façade:
Facade is a structural pattern, which identifies a simple way to realize relationships between entities.

Def: “Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.”

Explanation:

Facade discusses encapsulating a complex subsystem within a single interface object. This reduces the learning curve necessary to successfully leverage the subsystem. It also promotes decoupling the subsystem from its potentially many clients. On the other hand, if the Facade is the only access point for the subsystem, it will limit the features and flexibility that "power users" may need.

Class Diagram:

Sequence Diagram:



Advantages:
  • It hides the implementation of the subsystem from clients, making the subsystem easier to use.
  • It promotes weak coupling between the subsystem and its clients which allows changing the classes that comprise the subsystem without affecting the clients.
  • It reduces compilation dependencies in large software systems
  • It simplifies porting systems to other platforms, because it's less likely that building one subsystem requires building all others.
  • It does not prevent sophisticated clients from accessing the underlying classes

Disadvantages:
It does not prevent clients from accessing the underlying classes!

Applicability:

ü  To provide a simple interface to a complex subsystem. This interface is good enough for most clients; more sophisticated clients can look beyond the facade.

ü  To decouple the classes of the subsystem from its clients and other subsystems, thereby promoting subsystem independence and portability.

Examples:
#include <iostream>
using namespace std;

class Engine
{
public:
       void start()
       {
             cout << "start car engine" << endl;
       }
};
class lights
{
public:
       void turnlightsOn()
       {
             cout << "Turn head lights ON" << endl;
       }
};
class Car
{
private:
       Engine e;
       lights l;
public:
   void startCar()
   {
          e.start();
          l.turnlightsOn();
   }
};

int main() {
    cout << "Facade pattern example" << endl;
    cout << "***********************" << endl;
    Car c;
    c.startCar();
    cout << "***********************" << endl;
    return 0;
}

//============================================================================
// Name        : facade_example1.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include<iostream>
#include<string>
using namespace std;
/*************************
 * Customer class
 **************************/
class Customer
{
public:
       //copy constructor
       Customer (const string& name) : custname(name){}

       //return customer name
       const string& getcustomerName(void)
       {
             return custname;
       }
private:
       Customer(); //not allowed
       string custname;
};

/*************************
 * Bank class
 **************************/
class Bank
{
public:
       bool HasBalance(Customer c, int amount)
       {
             cout << "Check bank for " <<c.getcustomerName()<<endl;
             return true;
       }
};
/*************************
 * Credit  class
 **************************/
class Credit
{
public:
       bool HasValidCredit(Customer c, int amount)
       {
             cout << "Check credit for " <<c.getcustomerName()<<endl;
             return true;
       }
};
/*************************
 * Loan  class
 **************************/
class Loan
{
public:
       bool CreditHistory(Customer c, int amount)
       {
             cout << "Check loans for " <<c.getcustomerName()<<endl;
             return true;
       }
};

/*************************
 * Facade  class
 **************************/
class Mortgage
{
public:
       bool IsEligible(Customer cust, int amount)
       {
             cout << cust.getcustomerName() << " applies for a loan for " << amount <<endl;
             bool eligible = true;

             eligible = custbank.HasBalance(cust, amount);

             if(eligible)
                    eligible = custloan.CreditHistory(cust, amount);

             if(eligible)
                    eligible = custcredit.HasValidCredit(cust, amount);

             return eligible;
       }

private:
       Bank custbank;
       Loan custloan;
       Credit custcredit;
};

//The Main method
int main()
{
       Mortgage mortgage;
       Customer customer("rvk");
       bool eligible = mortgage.IsEligible(customer, 100000);
       cout << "\n" << customer.getcustomerName() << " has been " << (eligible ? "Approved" : "Rejected") << endl;

       return 0;
}


Generalized Example:
// "Subsystem ClassA"
class SubSystemOne
{
public:
    void MethodOne()
    {
       std::cout << " SubSystemOne Method" << std::endl;
    }
};

// Subsystem ClassB"

class SubSystemTwo
{
public:
    void MethodTwo()
    {
       std::cout << " SubSystemTwo Method" << std::endl;
    }
};

// Subsystem ClassC"

class SubSystemThree
{
public:
    void MethodThree()
    {
             std::cout << " SubSystemThree Method" << std::endl;
    }
};

// Subsystem ClassD"

class SubSystemFour
{
public:
    void MethodFour()
    {
       std::cout << " SubSystemFour Method" << std::endl;
    }
};

// "Facade"

class Facade
{
    SubSystemOne one;
    SubSystemTwo two;
    SubSystemThree three;
    SubSystemFour four;

public:
    Facade()
    {
    }

    void MethodA()
    {
       std::cout << "\nMethodA() ---- " << std::endl;
       one.MethodOne();
       two.MethodTwo();
       four.MethodFour();
    }
    void MethodB()
    {
       std::cout << "\nMethodB() ---- " << std::endl;
       two.MethodTwo();
       three.MethodThree();
    }
};

int Main()
{
    Facade facade = new Facade();

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

    return 0;
}


Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Technical Articles - Skyblue - Powered by Blogger - Designed by Johanes Djogan -