Monday, December 19, 2011

C# Facade Design Pattern

In real world a facade or façade  is generally one exterior side of a building usually, but not always, the front...:)

The facade pattern is a software engineering design pattern commonly used with object oriented programming analogy to an architectural facade.A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can:
  • make a software library easier to use, understand and test, since the facade has convenient methods for common tasks;
  • make code that uses the library more readable, for the same reason;
  • reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
  • wrap a poorly-designed collection of APIs with a single well-designed API (as per task needs).
    • Facade defines a higher-level interface that makes the subsystem easier to use. Facade  is one of the common and easiest patterns. It wrap a complicated subsystem with a simpler interface. As a developer we often use this facade pattern. Even some of them don't recognise while using it that its a facade patterns. I have tried to explain facade pattern by building a blog system.
    • Facade design patterns  gives easier interface for common task. It provides simplified and uniform interface to a large subsystem of classes. For example in a shopping cart system  there could be many API’s related to register user, purchase,user detail, transactions etc so facade deals with all these interfaces and provide one simple modified interface to deals with them.
      A more simple non-software example can be if you want to play a game suppose Metal gear Solid on PS3 (Did i tell you that i love that game?). You Require PS3, HD TV, Controller etc.  So there are so many sub system could be involve if you want to play a game on ps3. But actually all you are doing in simpler term is playing a game. So facade can give you a simple interface with a operation call PlayGame() in this example.The Facade object should be a fairly simple advocate or facilitator. If the Facade is the only access point for the subsystem, it will limit the features and flexibility that “power users” may need.
      For example we have
      // Facade pattern -- Structural example

      using System;

      namespace DoFactory.GangOfFour.Facade.Structural
      {
        /// <summary>
        /// MainApp startup class for Structural
        /// Facade Design Pattern.
        /// </summary>
        class MainApp
        {
          /// <summary>
          /// Entry point into console application.
          /// </summary>
          public static void Main()
          {
            Facade facade = new Facade();

            facade.MethodA();
            facade.MethodB();

            // Wait for user
            Console.ReadKey();
          }
        }

        /// <summary>
        /// The 'Subsystem ClassA' class
        /// </summary>
        class SubSystemOne
        {
          public void MethodOne()
          {
            Console.WriteLine(" SubSystemOne Method");
          }
        }

        /// <summary>
        /// The 'Subsystem ClassB' class
        /// </summary>
        class SubSystemTwo
        {
          public void MethodTwo()
          {
            Console.WriteLine(" SubSystemTwo Method");
          }
        }

        /// <summary>
        /// The 'Subsystem ClassC' class
        /// </summary>
        class SubSystemThree
        {
          public void MethodThree()
          {
            Console.WriteLine(" SubSystemThree Method");
          }
        }

        /// <summary>
        /// The 'Subsystem ClassD' class
        /// </summary>
        class SubSystemFour
        {
          public void MethodFour()
          {
            Console.WriteLine(" SubSystemFour Method");
          }
        }

        /// <summary>
        /// The 'Facade' class
        /// </summary>
        class Facade
        {
          private SubSystemOne _one;
          private SubSystemTwo _two;
          private SubSystemThree _three;
          private SubSystemFour _four;

          public Facade()
          {
            _one = new SubSystemOne();
            _two = new SubSystemTwo();
            _three = new SubSystemThree();
            _four = new SubSystemFour();
          }

          public void MethodA()
          {
            Console.WriteLine("\nMethodA() ---- ");
            _one.MethodOne();
            _two.MethodTwo();
            _four.MethodFour();
          }

          public void MethodB()
          {
            Console.WriteLine("\nMethodB() ---- ");
            _two.MethodTwo();
            _three.MethodThree();
          }
        }
      }

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

      MethodB() ----
      SubSystemTwo Method
      SubSystemThree Method

No comments: