• Source
    1. //Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
    2. //Façade is part of Structural Patterns
    3. //Structural Patterns deal with decoupling the interface and implementation of classes and objects
    4. //A Facade is single class that represents an entire subsystem
    5.  
    6. //The example we consider here is a case of a customer applying for mortgage
    7. //The bank has to go through various checks to see if Mortgage can be approved for the customer
    8. //The facade class goes through all these checks and returns if the morgage is approved
    9.  
    10. #include<iostream>
    11. #include<string>
    12.  
    13. using namespace std;
    14.  
    15. // Customer class
    16. class Customer
    17. {
    18. public:
    19. Customer (const string& name) : name_(name){}
    20. const string& Name(void)
    21. {
    22. return name_;
    23. }
    24. private:
    25. Customer(); //not allowed
    26. string name_;
    27. };
    28.  
    29. // The 'Subsystem ClassA' class
    30. class Bank
    31. {
    32. public:
    33. bool HasSufficientSavings(Customer c, int amount)
    34. {
    35. cout << "Check bank for " <<c.Name()<<endl;
    36. return true;
    37. }
    38. };
    39.  
    40. // The 'Subsystem ClassB' class
    41. class Credit
    42. {
    43. public:
    44. bool HasGoodCredit(Customer c, int amount)
    45. {
    46. cout << "Check credit for " <<c.Name()<<endl;
    47. return true;
    48. }
    49. };
    50.  
    51. // The 'Subsystem ClassC' class
    52. class Loan
    53. {
    54. public:
    55. bool HasGoodCredit(Customer c, int amount)
    56. {
    57. cout << "Check loans for " <<c.Name()<<endl;
    58. return true;
    59. }
    60. };
    61.  
    62. // The 'Facade' class
    63. class Mortgage
    64. {
    65. public:
    66. bool IsEligible(Customer cust, int amount)
    67. {
    68. cout << cust.Name() << " applies for a loan for $" << amount <<endl;
    69. bool eligible = true;
    70.  
    71. eligible = bank_.HasSufficientSavings(cust, amount);
    72.  
    73. if(eligible)
    74. eligible = loan_.HasGoodCredit(cust, amount);
    75.  
    76. if(eligible)
    77. eligible = credit_.HasGoodCredit(cust, amount);
    78.  
    79. return eligible;
    80. }
    81.  
    82. private:
    83. Bank bank_;
    84. Loan loan_;
    85. Credit credit_;
    86. };
    87.  
    88. //The Main method
    89. int main()
    90. {
    91. Mortgage mortgage;
    92. Customer customer("Brad Pitt");
    93.  
    94. bool eligible = mortgage.IsEligible(customer, 1500000);
    95.  
    96. cout << "\n" << customer.Name() << " has been " << (eligible ? "Approved" : "Rejected") << endl;
    97.  
    98. return 0;
    99. }
    100.  
    101. // See more at: http://borisivanov.com/index.php/2016/03/26/facade/