Facade Pattern

 

Comments

 

Create manager/facade class to handle calculations from passed in object i.e. Person/Customer

 

Code

 

 

1.	using System;
2.	 
3.	namespace DoFactory.GangOfFour.Facade.RealWorld
4.	{
5.	  /// 
6.	  /// MainApp startup class for Real-World 
7.	  /// Facade Design Pattern.
8.	  /// 
9.	  class MainApp
10.	  {
11.	    /// 
12.	    /// Entry point into console application.
13.	    /// 
14.	    static void Main()
15.	    {
16.	      // Facade
17.	      Mortgage mortgage = new Mortgage();
18.	 
19.	      // Evaluate mortgage eligibility for customer
20.	      Customer customer = new Customer("Ann McKinsey");
21.	      bool eligible = mortgage.IsEligible(customer, 125000);
22.	 
23.	      Console.WriteLine("\n" + customer.Name +
24.	          " has been " + (eligible ? "Approved" : "Rejected"));
25.	 
26.	      // Wait for user
27.	      Console.ReadKey();
28.	    }
29.	  }
30.	 
31.	  /// 
32.	  /// The 'Subsystem ClassA' class
33.	  /// 
34.	  class Bank
35.	  {
36.	    public bool HasSufficientSavings(Customer c, int amount)
37.	    {
38.	      Console.WriteLine("Check bank for " + c.Name);
39.	      return true;
40.	    }
41.	  }
42.	 
43.	  /// 
44.	  /// The 'Subsystem ClassB' class
45.	  /// 
46.	  class Credit
47.	  {
48.	    public bool HasGoodCredit(Customer c)
49.	    {
50.	      Console.WriteLine("Check credit for " + c.Name);
51.	      return true;
52.	    }
53.	  }
54.	 
55.	  /// 
56.	  /// The 'Subsystem ClassC' class
57.	  /// 
58.	  class Loan
59.	  {
60.	    public bool HasNoBadLoans(Customer c)
61.	    {
62.	      Console.WriteLine("Check loans for " + c.Name);
63.	      return true;
64.	    }
65.	  }
66.	 
67.	  /// 
68.	  /// Customer class
69.	  /// 
70.	  class Customer
71.	  {
72.	    private string _name;
73.	 
74.	    // Constructor
75.	    public Customer(string name)
76.	    {
77.	      this._name = name;
78.	    }
79.	 
80.	    // Gets the name
81.	    public string Name
82.	    {
83.	      get { return _name; }
84.	    }
85.	  }
86.	 
87.	  /// 
88.	  /// The 'Facade' class
89.	  /// 
90.	  class Mortgage
91.	  {
92.	    private Bank _bank = new Bank();
93.	    private Loan _loan = new Loan();
94.	    private Credit _credit = new Credit();
95.	 
96.	    public bool IsEligible(Customer cust, int amount)
97.	    {
98.	      Console.WriteLine("{0} applies for {1:C} loan\n",
99.	        cust.Name, amount);
100.	 
101.	      bool eligible = true;
102.	 
103.	      // Check creditworthyness of applicant
104.	      if (!_bank.HasSufficientSavings(cust, amount))
105.	      {
106.	        eligible = false;
107.	      }
108.	      else if (!_loan.HasNoBadLoans(cust))
109.	      {
110.	        eligible = false;
111.	      }
112.	      else if (!_credit.HasGoodCredit(cust))
113.	      {
114.	        eligible = false;
115.	      }
116.	 
117.	      return eligible;
118.	    }
119.	  }
120.	}

Output

 

Ann McKinsey applies for $125,000.00 loan

Check bank for Ann McKinsey
Check loans for Ann McKinsey
Check credit for Ann McKinsey

Ann McKinsey has been Approved