Skip to main content

Facade Design Pattern - C#

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

Comments

Popular posts from this blog

Accessing File Stored in Windows Azure Blob Storage Using jQuery

Did you know it was possible to access the Windows Azure Blob Storage directly from JavaScript, for example using jQuery? At first, it sounds obvious, since Blobs are after all accessible from a public UR. But in practice, there is a very big hurdle: the Web browser’s Same Origine Policy or SOP, that restricts JavaScript code to accessing resources originating from the same site the script was loaded from. This means that you will never be able to load a Windows Azure Blob using XMLHttpRequest for example! Fortunately, there is a popular workaround called JSONP (“JSON with Padding”). The idea behind this technique is that the script tag is not submitted to the SOP: an HTML page can thus load a JavaScript file from any site. So, if you expose your data in an “executable” form in JavaScript, a page will be able to load this data using a script tag. For example: <script type=”text/javascript” src=”http://www.sandeepknarware.in/exemple.jsonp”> </script> But how can ...

Support for debugging lambda expressions with Visual Studio 2015

Anyone who uses LINQ (or lambdas in general) and the debugger will quickly discover the dreaded message “Expression cannot contain lambda expressions”. Lack of lambda support has been a limitation of the Visual Studio Debugger ever since Lambdas were added to C# and Visual Basic.  With visual studio 2015 Microsoft has added support for debugging lambda expressions. Let’s first look at an example, and then I’ll walk you through current limitations. Example To try this yourself, create a new C# Console app with this code: using System.Diagnostics; using System.Linq; class Program { static void Main() { float[] values = Enumerable.Range(0, 100).Select(i => (float)i / 10).ToArray(); Debugger.Break(); } } Then compile, start debugging, and add “values.Where(v => (int)v == 3).ToArray()” in the Watch window. You’ll be happy to see the same as what the screenshot above shows you. I am using Visual Studio 2015 Preview and it has some limitati...

gcAllowVeryLargeObjects Element

There are numerous new features coming with .NET 4.5 and here, on this blog, you can find several posts about it. But the feature we are goint to talk about today is very exciting, because we were waiting for it more than 10 years. Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio 2012, and create C# Console Application, like picture below. First lets create simple struct with two double members like example below: 1 2 3 4 5 6 7 8 9 10 11 12 public struct ComplexNumber {      public double Re;      public double Im;      public ComplexNumber( double re, double im)      {    ...