How to use the Facade design template in C #

0

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

The facade design template is a creative design template, that is, it belongs to the creative design template group and can be used to represent a gateway to a subsystem. By using the facade design template, you can provide a simple, unified high-level interface for a set of interfaces that make the subsystems decoupled from the customer and also ensure 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 pattern is a great choice. By using the facade design template in this case, you can eliminate the need for the client to directly call complex system methods and instead provide an interface with which the client can communicate.

The facade template is also a good candidate if the subsystem contains many different methods when you only need a few of those methods – you can use the facade design template in this case and provide only access to only the methods that are needed through a high level interface. Here is 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 top-level interface that makes the subsystem easier to use.”

Facade design model implementation in C #

We’ve covered the concepts – now let’s take a look at some code. In this section, we will discuss how we can implement the facade design pattern using C #. Participants in a typical facade design model implementation 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 next class named SubsystemA which 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 is the Facade class. Notice how the instances of the two subsystems we created earlier were created in the Facade class. There are two methods, namely Operation1 and Operation2. While the first is about validating user credentials by taking advantage of the ValidateUser method of the SubsystemA class, the second is about validating the credit card number and then making a payment using of the validated credit card. To validate the credit card number, the ValidateCreditCard method of the SubsystemB class is called from the Operation2 method. Then, 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);

        }

    }

Okay, so what’s the next step? All we have to do is 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 the intricacies and internal complexities are extracted from the consumer when we use the facade design template. All the customer or consumer needs to know are the methods of the facade to call.

Copyright © 2015 IDG Communications, Inc.


Source link

Share.

Leave A Reply