This real-world code demonstrates the Facade pattern as a MortgageApplication object which provides a simplified interface to a large subsystem of classes measuring the creditworthyness of an applicant.
participants
The classes and/or objects participating in this pattern are:
Facade (MortgageApplication)
knows which subsystem classes are responsible for a request.
delegates client requests to appropriate subsystem objects.
Subsystem classes (Bank, Credit, Loan)
implement subsystem functionality.
handle work assigned by the Facade object.
have no knowledge of the facade and keep no reference to it.
// Facade pattern -- Real World example
using System;
namespace DoFactory.GangOfFour.Facade.RealWorld { // MainApp test application
class MainApp { staticvoid
Main
() { // Facade Mortgage mortgage = new Mortgage();
// Evaluate mortgage eligibility for customer Customer customer = new Customer("Ann McKinsey"); bool eligable = mortgage.IsEligible(customer,125000);
Console.WriteLine("/n" + customer.Name + " has been " + (eligable ? "Approved" : "Rejected"));
// Wait for user Console.Read(); } }
// "Subsystem ClassA"
class Bank { publicbool HasSufficientSavings(Customer c, int amount) { Console.WriteLine("Check bank for " + c.Name); returntrue; } }
// "Subsystem ClassB"
class Credit { publicbool HasGoodCredit(Customer c) { Console.WriteLine("Check credit for " + c.Name); returntrue; } }
// "Subsystem ClassC"
class Loan { publicbool HasNoBadLoans(Customer c) { Console.WriteLine("Check loans for " + c.Name); returntrue; } }
class Customer { privatestring name;
// Constructor public Customer(string name) { this.name = name; }