Monday, August 18, 2008

Façade pattern

Design patterns provide solutions to frequently occurring problems and also these patterns make the software efficient by suggesting effective ways.
FACADE is one the patterns which is commonly used.

What is Façade pattern ?
In this pattern the external access to a software system is made organized and efficient. A system containing no of sub systems (modules) can exposes many interfaces to the
external systems (clients ). Frequently for getting access to the required module, intelligence needs to be added at client side only. It requires internal knowledge of the
system being accessed and could make the client side little thick.
Facade pattern makes the system to expose only single interface i.e. point of contact and internally makes the decision about the redirection of the client request to the
correct module.

Advantages?
Thus the system access is more organized and easy for the client system.

The below example shows a Facade implementation. In this, there could be no of schemes in account system which have different formulae for interest rates. Depending upon the passed scheme code, correct scheme function is invoked to compute the interest rate.


#include "stdafx.h"
#include "iostream.h"

enum Scheme { A, B} ;

class SchemeA
{
public :

double GetInterest(float dbAmt, int intTenure)
{
return ( 0.02 * dbAmt / intTenure );
}
};


class SchemeB
{
public :

double GetInterest(float dbAmt, int intTenure)
{
return ( 0.03 * dbAmt / intTenure );
}
};

// Facade class with interface GetInterest(...)

class Facade
{
public:

double GetInterest(Scheme SchemeType, float dbAmt, int intTenure)
{

// decision making for reditrection of the client request
if (SchemeType == A)
{
SchemeA objA;

return (objA.GetInterest(dbAmt, intTenure));
}

if (SchemeType == B)
{
SchemeA objB;

return (objB.GetInterest( dbAmt, intTenure));
}

}
};


// client system for the above Facaded system
int main(int argc, char* argv[])
{
Facade objFacade;

// call to the Facade interface
cout<< objFacade.GetInterest(A, 5000, 14);
return 0;
}

No comments: