Wednesday 29 September 2010

C++ example for Facade Design Pattern



Frequently, as your programs evolve and develop, they grow in complexity. In fact, for all the excitement about using design patterns, these patterns sometimes generate so many classes that it is difficult to understand the program’s flow. Furthermore, there may be a number of complicated subsystems, each of which has its own complex interface.

The Façade pattern allows you to simplify this complexity by providing a simplified interface to these subsystems. This simplification may in some cases reduce the flexibility of the underlying classes, but usually provides all the function needed for all but the most sophisticated users. These users can still, of course, access the underlying classes and methods.

Facade takes a “riddle wrapped in an enigma shrouded in mystery”, and interjects a wrapper that tames the amorphous and inscrutable mass of software.

The frequency of use for Facade is very high.

C++ example as follows:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Façade is part of Structural Patterns
//Structural Patterns deal with decoupling the interface and implementation of classes and objects
//A Facade is single class that represents an entire subsystem

//The example we consider here is a case of a customer applying for mortgage
//The bank has to go through various checks to see if Mortgage can be approved for the customer
//The facade class goes through all these checks and returns if the morgage is approved

#include<iostream>
#include<string>

using namespace
std;

// Customer class
class Customer
{

public
:
Customer (const string& name) : name_(name){}
const
string& Name(void)
{

return
name_;
}

private
:
Customer(); //not allowed
string name_;
};


// The 'Subsystem ClassA' class
class Bank
{

public
:
bool
HasSufficientSavings(Customer c, int amount)
{

cout << "Check bank for " <<c.Name()<<endl;
return
true;
}
};


// The 'Subsystem ClassB' class
class Credit
{

public
:
bool
HasGoodCredit(Customer c, int amount)
{

cout << "Check credit for " <<c.Name()<<endl;
return
true;
}
};


// The 'Subsystem ClassC' class
class Loan
{

public
:
bool
HasGoodCredit(Customer c, int amount)
{

cout << "Check loans for " <<c.Name()<<endl;
return
true;
}
};


// The 'Facade' class
class Mortgage
{

public
:
bool
IsEligible(Customer cust, int amount)
{

cout << cust.Name() << " applies for a loan for $" << amount <<endl;
bool
eligible = true;

eligible = bank_.HasSufficientSavings(cust, amount);

if
(eligible)
eligible = loan_.HasGoodCredit(cust, amount);

if
(eligible)
eligible = credit_.HasGoodCredit(cust, amount);

return
eligible;
}


private
:
Bank bank_;
Loan loan_;
Credit credit_;
};


//The Main method
int main()
{

Mortgage mortgage;
Customer customer("Brad Pitt");

bool
eligible = mortgage.IsEligible(customer, 1500000);

cout << "\n" << customer.Name() << " has been " << (eligible ? "Approved" : "Rejected") << endl;

return
0;
}



The output is as follows:


More on Facade:

http://sourcemaking.com/design_patterns/facade

http://www.patterndepot.com/put/8/facade.pdf

http://sourcemaking.com/design_patterns/facade

2 comments:

  1. Just a quick note on this code...

    loan_.HasGoodCredit(cust, amount);
    credit_.HasGoodCredit(cust, amount);

    should be:

    eligible = loan_.HasGoodCredit(cust, amount);
    eligible = credit_.HasGoodCredit(cust, amount);

    Other than that, this is a pretty cool Design Pattern. Thank you for posting!

    ReplyDelete
  2. Justin, thanks for pointing it out. It took me a while but it has been fixed now.

    ReplyDelete