Sunday, August 21, 2016

Facade Pattern


Facade pattern provides simpler way to users to access complex subsystems. Facade pattern falls under structural pattern of GOF (Gang of Four) patterns.

When to use –


Facade pattern can be used when you have to deal with multiple classes having complex and difficult logic. Facade class acts as “wrapper” class and provides easy to understand and simple to use interface to client. Facade object decouples the subsystems from the client. Client doesn’t have information about how subsystems objects are created and its methods are called in Facade class. Facade class provides client a simpler way to access subsystems. Facade pattern can also be used when you need entry point to multiple complex subsystems.

Facade pattern can also be ideal choice when you are dealing with multiple legacy systems and you can’t change code of legacy systems due to code unavailability or multiple systems are impacted by code change.

Major components of Facade Pattern –


Facade – This is a class and acts as wrapper for delegating client request to respective subsystems.
Subsystems – This are subsystem classes which contains functionality that client wants.

There are so many real world example available for Facade pattern but most common example is Reception counter or “May I Help you” counter at any company or bank. So client just need to contact to reception counter and he/she will be redirected to respective department to get his/her work done. See below example of Facade pattern.

Code –
//subsystem-1
public class SavingsAccount
{
    public void OpenNewAccount(string name)
    {
        Console.WriteLine(string.Format("{0}, welcome to savings account department", name));
    }
}
//subsystem-2
public class CreditCard
{
    public void ApplyCreditCard(string name)
    {
        Console.WriteLine(string.Format("{0}, welcome to credit card department.", name));
    }
}
//subsystem-3
public class PersonalLoan
{
    public void GetPersonalLoan(string name)
    {
        Console.WriteLine(string.Format("{0}, welcome to personal loan department", name));
    }
}
//Facade
public class Reception
{
    private string Name;
    private SavingsAccount savingsaccount = new SavingsAccount();
    private CreditCard creditCard = new CreditCard();
    private PersonalLoan personalLoan = new PersonalLoan();
    public Reception(string name)
    {
        Name = name;
    }
    public void GetPersonalLoan()
    {
        personalLoan.GetPersonalLoan(Name);
    }
    public void ApplyForCreditCard()
    {
        creditCard.ApplyCreditCard(Name);
    }
    public void OpenSavingsAccount()
    {
        savingsaccount.OpenNewAccount(Name);
    }
}
//client
class Program
{
    static void Main(string[] args)
    {
        Reception reception = new Reception("Mitesh");
        reception.OpenSavingsAccount();
        reception.ApplyForCreditCard();
        reception.GetPersonalLoan();

        Console.ReadLine();
    }
}

Output –




As you can see in above example, Reception class acts as Facade (wrapper) class which is entry point for client to connect to multiple subsystems for his/her different types of need like opening savings account, get personal loan, apply for credit card etc.

I hope this article helps you to know more about Facade Pattern. Please leave your feedback in comments section below.


References –

See Also –


No comments:

Post a Comment