Design Pattern Tutorials

Facade Design Pattern

Definition

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

Frequency of use:
High




UML class diagram






Participants


    The classes and 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.



Structural code in C#


This structural code demonstrates the Facade pattern which provides a simplified and uniform interface to a large subsystem of classes.

  1. using System;

  2.  

  3. namespace DoFactory.GangOfFour.Facade.Structural

  4. {

  5.   /// <summary>

  6.   /// MainApp startup class for Structural

  7.   /// Facade Design Pattern.

  8.   /// </summary>

  9.   class MainApp

  10.   {

  11.     /// <summary>

  12.     /// Entry point into console application.

  13.     /// </summary>

  14.     public static void Main()

  15.     {

  16.       Facade facade = new Facade();

  17.  

  18.       facade.MethodA();

  19.       facade.MethodB();

  20.  

  21.       // Wait for user

  22.       Console.ReadKey();

  23.     }

  24.   }

  25.  

  26.   /// <summary>

  27.   /// The 'Subsystem ClassA' class

  28.   /// </summary>

  29.   class SubSystemOne

  30.   {

  31.     public void MethodOne()

  32.     {

  33.       Console.WriteLine(" SubSystemOne Method");

  34.     }

  35.   }

  36.  

  37.   /// <summary>

  38.   /// The 'Subsystem ClassB' class

  39.   /// </summary>

  40.   class SubSystemTwo

  41.   {

  42.     public void MethodTwo()

  43.     {

  44.       Console.WriteLine(" SubSystemTwo Method");

  45.     }

  46.   }

  47.  

  48.   /// <summary>

  49.   /// The 'Subsystem ClassC' class

  50.   /// </summary>

  51.   class SubSystemThree

  52.   {

  53.     public void MethodThree()

  54.     {

  55.       Console.WriteLine(" SubSystemThree Method");

  56.     }

  57.   }

  58.  

  59.   /// <summary>

  60.   /// The 'Subsystem ClassD' class

  61.   /// </summary>

  62.   class SubSystemFour

  63.   {

  64.     public void MethodFour()

  65.     {

  66.       Console.WriteLine(" SubSystemFour Method");

  67.     }

  68.   }

  69.  

  70.   /// <summary>

  71.   /// The 'Facade' class

  72.   /// </summary>

  73.   class Facade

  74.   {

  75.     private SubSystemOne _one;

  76.     private SubSystemTwo _two;

  77.     private SubSystemThree _three;

  78.     private SubSystemFour _four;

  79.  

  80.     public Facade()

  81.     {

  82.       _one = new SubSystemOne();

  83.       _two = new SubSystemTwo();

  84.       _three = new SubSystemThree();

  85.       _four = new SubSystemFour();

  86.     }

  87.  

  88.     public void MethodA()

  89.     {

  90.       Console.WriteLine("\nMethodA() ---- ");

  91.       _one.MethodOne();

  92.       _two.MethodTwo();

  93.       _four.MethodFour();

  94.     }

  95.  

  96.     public void MethodB()

  97.     {

  98.       Console.WriteLine("\nMethodB() ---- ");

  99.       _two.MethodTwo();

  100.       _three.MethodThree();

  101.     }

  102.   }

  103. }

  104.  

Output
MethodA() ----
SubSystemOne Method
SubSystemTwo Method
SubSystemFour Method

MethodB() ----
SubSystemTwo Method
SubSystemThree Method




Real-world code in C#


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.

  1. using System;

  2.  

  3. namespace DoFactory.GangOfFour.Facade.RealWorld

  4. {

  5.   /// <summary>

  6.   /// MainApp startup class for Real-World

  7.   /// Facade Design Pattern.

  8.   /// </summary>

  9.   class MainApp

  10.   {

  11.     /// <summary>

  12.     /// Entry point into console application.

  13.     /// </summary>

  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.   /// <summary>

  32.   /// The 'Subsystem ClassA' class

  33.   /// </summary>

  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.   /// <summary>

  44.   /// The 'Subsystem ClassB' class

  45.   /// </summary>

  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.   /// <summary>

  56.   /// The 'Subsystem ClassC' class

  57.   /// </summary>

  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.   /// <summary>

  68.   /// Customer class

  69.   /// </summary>

  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.   /// <summary>

  88.   /// The 'Facade' class

  89.   /// </summary>

  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. }

  121.  
  122.  

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

;