Printed from www.rmfusion.com A Developer website designed for Developers

Façade Pattern Code Review

Code Download

Define the Façade Object

Define the Facade class which provides a common view into the defined high-level systems (objects).

class Facade { private BankSystem bank = null; private LoanSystem loan = null; private CreditSystem credit = null;
public Facade() { bank = new BankSystem(); loan = new LoanSystem(); credit = new CreditSystem(); }
public Boolean IsEligible(String customerID, Int32 amount) { Boolean eligible = true;
Console.WriteLine(String.Format("Customer {0} applies for {1:C} loan\n", customerID, amount));
if (!bank.HasSufficientSavings(customerID)) { eligible = false; } else if (!loan.HasNoBadLoans(customerID)) { eligible = false; } else if (!credit.HasGoodCredit(customerID)) { eligible = false; } return (eligible); } }

The IsEligible() method will invoke separate methods in each individual object and return a single response.

Define the SubSystem Objects

The BankSystem class (object), CreditSystem class (object) and the LoanSystem class (object) represent the defined high-level systems (objects).

class BankSystem { public bool HasSufficientSavings(String customerID) { Console.WriteLine(String.Format("Checking banking system for {0}.", customerID)); return true; } } class CreditSystem { public bool HasGoodCredit(String customerID) { Console.WriteLine(String.Format("Checking credit system for {0}.", customerID)); return true; } } class LoanSystem { public bool HasNoBadLoans(String customerID) { Console.WriteLine(String.Format("Checking loan system for {0}.", customerID)); return true; } }

Use the Façade Pattern

The Program class creates an instance of the Façade class (object) and invokes the IsEligible() method.

class Program { static void Main(string[] args) { String customerID = "ABN000726351";
Facade systemFacade = new Facade();
Boolean eligible = systemFacade.IsEligible(customerID, 120000);
Console.WriteLine(String.Format("\n{0} has been {1}.", customerID, (eligible ? "Approved" : "Rejected")));
Console.Read(); } }