Facade Design Pattern


Sample 1:
Facade pattern falls under Structural Pattern of Gang of Four (GOF) Design Patterns in .Net. The Facade design pattern is particularly used when a system is very complex or difficult to understand because system has a large number of interdependent classes or its source code is unavailable. In this article, I would like share what is facade pattern and how is it work?

What is facade Pattern

Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.

This pattern involves a single wrapper class which contains a set of members which are required by client. These members access the system on behalf of the facade client and hide the implementation details.

The facade design pattern is particularly used when a system is very complex or difficult to understand because system has a large number of interdependent classes or its source code is unavailable.

Facade Pattern – UML Diagram & Implementation

The UML class diagram for the implementation of the facade design pattern is given below:

The classes, interfaces and objects in the above UML class diagram are as follows:

  1. Complex System

    A library of subsystems.

  2. SubsystemA, SubsystemB, SubsystemC

    These are classes within complex system and offer detailed operations.

  3. Façade

    This is a wrapper class which wrapper class which contains a set of members which are required by client.

  4. Client

    This is a class which calls the high-level operations in the Façade.

C# – Implementation Code

  1. class SubsystemA
  2. {
  3. public string OperationA1()
  4. {
  5. return “Subsystem A, Method A1\n”;
  6. }
  7. public string OperationA2()
  8. {
  9. return “Subsystem A, Method A2\n”;
  10. }
  11. }
  12. class SubsystemB
  13. {
  14. public string OperationB1()
  15. {
  16. return “Subsystem B, Method B1\n”;
  17. }
  18.  
  19. public string OperationB2()
  20. {
  21. return “Subsystem B, Method B2\n”;
  22. }
  23. }
  24. class SubsystemC
  25. {
  26. public string OperationC1()
  27. {
  28. return “Subsystem C, Method C1\n”;
  29. }
  30.  
  31. public string OperationC2()
  32. {
  33. return “Subsystem C, Method C2\n”;
  34. }
  35. }
  36.  
  37. public class Facade
  38. {
  39. SubsystemA a = new SubsystemA();
  40. SubsystemB b = new SubsystemB();
  41. SubsystemC c = new SubsystemC();
  42. public void Operation1()
  43. {
  44. Console.WriteLine(“Operation 1\n” +
  45. a.OperationA1() +
  46. a.OperationA2() +
  47. b.OperationB1());
  48. }
  49. public void Operation2()
  50. {
  51. Console.WriteLine(“Operation 2\n” +
  52. b.OperationB2() +
  53. c.OperationC1() +
  54. c.OperationC2());
  55. }
  56. }

Façade Pattern – Example

Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:

  1. CarModel, CarEngine, CarBody, CarAccessories – These are subsystems.
  2. CarFacade– Facade class.

C# – Sample Code

  1. /// <summary>
  2. /// The ‘Subsystem ClassA’ class
  3. /// </summary>
  4. class CarModel
  5. {
  6. public void SetModel()
  7. {
  8. Console.WriteLine(” CarModel – SetModel”);
  9. }
  10. }
  11.  
  12. /// <summary>
  13. /// The ‘Subsystem ClassB’ class
  14. /// </summary>
  15. class CarEngine
  16. {
  17. public void SetEngine()
  18. {
  19. Console.WriteLine(” CarEngine – SetEngine”);
  20. }
  21. }
  22.  
  23. /// <summary>
  24. /// The ‘Subsystem ClassC’ class
  25. /// </summary>
  26. class CarBody
  27. {
  28. public void SetBody()
  29. {
  30. Console.WriteLine(” CarBody – SetBody”);
  31. }
  32. }
  33.  
  34. /// <summary>
  35. /// The ‘Subsystem ClassD’ class
  36. /// </summary>
  37. class CarAccessories
  38. {
  39. public void SetAccessories()
  40. {
  41. Console.WriteLine(” CarAccessories – SetAccessories”);
  42. }
  43. }
  44.  
  45. /// <summary>
  46. /// The ‘Facade’ class
  47. /// </summary>
  48. public class CarFacade
  49. {
  50. CarModel model;
  51. CarEngine engine;
  52. CarBody body;
  53. CarAccessories accessories;
  54.  
  55. public CarFacade()
  56. {
  57. model = new CarModel();
  58. engine = new CarEngine();
  59. body = new CarBody();
  60. accessories = new CarAccessories();
  61. }
  62.  
  63. public void CreateCompleteCar()
  64. {
  65. Console.WriteLine(“******** Creating a Car **********\n”);
  66. model.SetModel();
  67. engine.SetEngine();
  68. body.SetBody();
  69. accessories.SetAccessories();
  70.  
  71. Console.WriteLine(“\n******** Car creation complete **********”);
  72. }
  73. }
  74.  
  75. /// <summary>
  76. /// Facade Pattern Demo
  77. /// </summary>
  78. class Program
  79. {
  80. static void Main(string[] args)
  81. {
  82. CarFacade facade = new CarFacade();
  83.  
  84. facade.CreateCompleteCar();
  85.  
  86. Console.ReadKey();
  87.  
  88. }
  89. }

facade Pattern Demo – Output

When to use it?

  1. A simple interface is required to access to a complex system.
  2. The abstractions and implementations of a subsystem are tightly coupled.
  3. Need an entry point to each level of layered software.
  4. The facade design pattern is particularly used when a system is very complex or difficult to understand because system has a large number of interdependent classes or its source code is unavailable

    Sample 2:

    Facade

     


     

    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

    Credits:
    http://www.dotnet-tricks.com/Tutorial/designpatterns/1N4c140713-Facade-Design-Pattern—C#.html
    http://www.dofactory.com/net/facade-design-pattern

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s