Implemention of the Facade Design Pattern

Facade Design Pattern

In this article we will discuss the need and the implementation of the Facade design pattern which is a structural design pattern. Facade simplifies useg of a class which has an otherwise complex interface.
The Facade Design pattern is formally defined as "Provide a unified interface to a set of interfaces in a system. Facade defines a higher-level interface that makes the subsystem easier to use."
First of all let's discuss the need of the Facade pattern.

Why do we need Facade pattern?

To answer the above question let's take an example:

Let's say you have designed an amazing library application in C# that anybody can use to integrate ecommerce functionality in their application.  You are very happy about the application as you have shown the application to end users who are very happy about it.
Now you distribute your component to various developers who need to integrate it into their applications. You have prepared some good documention that describes use of your component.
But all of a sudden you are hearing complaints from various developers about how your e-commerce component is encountering exceptions and failing now and then. You see the error log only to find that various developers are not using the component correctly. One user is calling the IsValidUser () method then the ShipProduct() () method then the TranferAmount () method, while the correct call sequence should be:

IsValidUser(), TranferAmount (), ShipProduct ()
You think maybe you had not exposed all these methods and now it's too late, right? No, maybe Façade can still help you.


Facade Simplifies Interface to the Complicated Class

We can solve the above problem using the façade pattern as described below. The BuyProduct class contains the following methods:

  1. IsValidUser
  2. TranferAmount
  3. ShipProduct

class BuyProduct


        public bool IsValidUser(string name){…}

        public bool  TranferAmount(decimal amount)  {…  }

        public bool ShipProduct(string address){…}



Each of the above methods returns a bool value and the success of one operation determines whether the next operation should be executed. Furthermore the operations should execute in the sequence specified above. So we need to enforce that the methods are called in the above sequence only. To enforce this we use the Façade pattern. We define the Façade class as just a single method that the clients need to call instead of three separate methods. Also the facade encapsulates the logic for using the BuyProduct class so the client is shielded from those details.

public bool BuyAndShipProduct(string name="",decimal amount=0,string address="")


            BuyProduct product=new BuyProduct();

            bool success = false;







                       success = true;




            return success;


The attached application contains the code for the client, BuyProduct class and the Facade class.