joydip_kanjilal
Contributor

How to use the Facade design pattern in C#

opinion
Dec 10, 20154 mins
Software Development

Take advantage of the facade design pattern to provide a simplified interface to a set of sub systems and hence reduce the dependencies and complexities in your designs

Design patterns have evolved to provide solutions to recurring problems and complexities in software design. These are categorized into three distinct categories namely, Creational, Structural and Behavioural. Creational patterns are used to create and manage the mechanism of creating instances of classes. Structural patterns are used to realize the relationships among the entities. Behavioural design patterns deal with object collaboration and delegation of responsibilities.

The facade design pattern is a creational design pattern, i.e., falls under the creational design pattern group and can be used to represent a gateway to a subsystem. In using the facade design pattern you can provide a simple and unified high level interface for a set of interfaces that make the sub systems decoupled from the client and also ensures that the sub systems are easy to use.

When should I use the façade design pattern?

You can use the facade pattern when you need to provide a simple interface as an entry point to access a complex system. In essence, if you have a complex system where the abstractions and the implementations of it are tightly coupled and you would not want the consumer to contact the complex system directly, the facade pattern is an excellent choice. In using the facade design pattern in this case, you can eliminate the need of the client to call the methods of the complex system directly and rather provide an interface to which the client can communicate.

The facade pattern is also a good candidate if the sub system contains many different methods while you would need just a few of those methods from among them — you can use the facade design pattern in this case and provide only access to only those methods that are needed through a high level interface. Here’s what the Gang of Four has to say on the facade design pattern: “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.”

Implementing the Façade design pattern in C#

We have covered the concepts — let’s now dig into some code. In this section we would discuss how we can implement the façade design pattern using C#. The participants in a typical implementation of the facade design pattern include the Facade and the Subsystem classes.

Now, consider two subsystems named SubsystemA and SubsystemB. While the former is used to validate user credentials, the latter is used to validate a credit card number and pay an amount using the credit card. The following class named SubsystemA that contains a method called ValidateUser.

class SubsystemA

    {

        public void ValidateUser(string userName, string password)

        {

            Console.WriteLine("Validate user credentials...");

        }

    }

The SubsystemB class is shown below – it contains two methods namely, ValidateCreditCard and PayAmount.

class SubsystemB

    {

        public void ValidateCreditCard(string cardNumber)

        {

            Console.WriteLine("Validate credit card...");

        }

        public void PayAmount(string cardNumber, double amount)

        {

            Console.WriteLine("Pay amount...");

        }

    }

And, here’s the Facade class. Note how the instances of the two sub systems we created earlier have been created inside the Facade class. There are two methods namely Operation1 and Operation2. While the former relates to validating the user credentials by taking advantage of the ValidateUser method of the SubsystemA class, the latter relates to validating the credit card number and then making a payment using the validated credit card. To validate the credit card number, the ValidateCreditCard method of SubsystemB class is called from within the Operation2 method. Next, the PayAmount method of the SubsystemB class is called to make a payment.

public class Facade

    {

        SubsystemA firstSubsystem = new SubsystemA();

        SubsystemB secondSubsystem = new SubsystemB();

        public void Operation1(string userName, string password)

        {

            firstSubsystem.ValidateUser(userName, password);

        }

        public void Operation2(string cardNumber, double amount)

        {

            secondSubsystem.ValidateCreditCard("1234567890");

            secondSubsystem.PayAmount(cardNumber, amount);

        }

    }

OK, so what’s next? All we have to do now is instantiate the Facade class and then invoke the methods Operation1 and Operation2 respectively.

    class Program

    {

      static void Main(string[] args)

        {

            Facade facade = new Facade();

            facade.Operation1("Joydip", "Joydip123");

            facade.Operation2("1234567890", 100.00);

            Console.Read();

        }

     }

The sample program we implemented in this article illustrated how the internal intricacies and complexities are abstracted from the consumer when we use the façade design pattern. All that the client or the consumer needs to know is the methods of the façade that need to be called.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author