How to use facade design pattern in C#

Design patterns have evolved to provide solutions to recurring problems and complexities in software design. These are classified into three distinct categories, namely, creational, structural and behavioral. Creation templates are used to create and manage the mechanism for creating instances of classes. Structural models are used to realize relationships between entities. Behavioral design patterns deal with the collaboration of objects and the delegation of responsibilities.

The facade design pattern is a creative design pattern, i.e. it falls under the creative design pattern group and can be used to represent a gateway to a subsystem. By using the facade design pattern, you can provide a simple, unified high-level interface for a set of interfaces that decouples subsystems from the client and also ensures that the subsystems are easy to use.

When should I use the facade design template?

You can use the facade template when you need to provide a simple interface as an entry point to access a complex system. Essentially, if you have a complex system where the abstractions and implementations of it are tightly coupled, and you wouldn’t want the consumer to directly contact the complex system, the facade model is an excellent choice. By using the facade design pattern in this case, you can eliminate the client’s need to call complex system methods directly and instead provide an interface that the client can communicate with.

The facade pattern is also a good candidate if the subsystem contains many different methods while you would only need a few of these methods among them – you can use the facade design pattern in this case and provide access only to the methods that are needed via a high-level interface. Here’s what the Gang of Four has to say about the facade design pattern: “Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier use.”

Implementing the Facade design pattern in C#

We’ve covered the concepts — now let’s dig into some code. In this section, we will discuss how we can implement the facade design pattern using C#. Participants in a typical implementation of the facade design pattern include the Facade and 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 which contains a method called ValidateUser.

class SubsystemA

    {

        public void ValidateUser(string userName, string password)

        {

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

        }

    }

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 is the Facade class. Notice how the instances of the two subsystems we created earlier were created inside the Facade class. There are two methods, namely Operation1 and Operation2. While the former is about validating the user credentials by leveraging the ValidateUser method of the SubsystemA class, the latter is about validating the credit card number and then paying using the card validated credit. To validate the credit card number, the ValidateCreditCard method of the SubsystemB class is called from 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 that remains is to instantiate the Facade class and then invoke the Operation1 and Operation2 methods 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 internal intricacies and complexities are removed from the consumer when we use the facade design pattern. All the client or consumer needs to know are the methods of the facade that need to be called.

Copyright © 2015 IDG Communications, Inc.

Abdul J. Gaspar