Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#
Tip/Trick

Unplugging Design Patterns - Facade

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
18 Jul 2012CPOL2 min read 17.9K   3   1
Facade is the most simple (arguably after Singleton) design pattern and makes programming beautiful and useful.

Introduction

Design patterns are extensively used as a programming methodology to develop extensible and robust applications. In this article, we will discuss and understand a design pattern Facade and evaluate its benefits in making an application scalable.

Background

Let us understand the design pattern with the help of an example.

Consider a Customer ABC who wishes to apply for a house loan from CBC Bank.

C#
public class Customer
{
    public string Name { get; set; }
    public string Occupation { get; set; }

    public Customer(string name, string occupation)
    {
        Name = name;
        Occupation = occupation;
    }
}

CBC Bank has a mortgage department which accepts house loan applications and evaluates their eligibility. Note that the customer only contacts the mortgage department to know whether he will be getting the loan or not.

Also, CBC Bank has two subdepartments:

  1. BankSubsytem that validates the customer background and determines if the customer is a valid customer.
  2. LoanSubsytem that determines the customer has no bad loans.
C#
public class Bank_Subsystem
{
    public bool IsValidCustomer(Customer cust)
    {
        Console.WriteLine("Check " + cust.Name + " is valid");
        return true;
    }
}
    
public class Loan_Subsystem
{
    public bool HasNoBadLoan(Customer cust)
    {
        Console.WriteLine("Check " + cust.Name + " for bad loans");
        return true;
    }
}

As per CBC Bank policy, the Mortgage department will contact BankSubsystem and LoanSubsystem to determine if the customer is valid and has no bad loans. If the customer is approved by both departments, the Mortgage department reckons he is eligible for a house loan.

C#
public class Mortgage_Facade
{
    private Bank_Subsystem bank;
    private Loan_Subsystem loan;
    
    public bool IsEligible(Customer cust)
    {
        bool isEligible = true;

        bank = new Bank_Subsystem();
        if (!bank.IsValidCustomer(cust))
        {
            isEligible = false;
        }

        loan = new Loan_Subsystem();
        if (isEligible && !loan.HasNoBadLoan(cust))
        {
            isEligible = false;
        }

        return isEligible;
    }
}

Note the interesting bit, the current interaction looks like this. The customer is not aware of either of the subsystems and the subsystems are not aware of the customer. Facade (the quintessential middlemen has all the right connections and does the job) knows about both the customer and the subsystems and does the task.

Sample Image

Consider the twist in the tale, a new regulation requires CBC Bank to also check the credit rating of the customer before approving the house loan. So, the Mortgage department should now also contact the credit subsystem which checks the credit worthiness of the customer.

C#
public class Credit_Subsystem
{
    public bool HasGoodCredit(Customer customer)
    {
        Console.WriteLine("Check "+customer.Name + " for good credit");
        return true;
    }
}

Wow, so what do we do now? Rewrite the entire application from scratch as the bank's policy has changed, or worry about changing each class? Well, absolutely no. Here's the beauty of Facade. We just add the CreditSubsytem class to our project and extend Mortgage_Facade.cs.

C#
public class Mortgage_Facade
{
    private Bank_Subsystem bank;
    private Loan_Subsystem loan;
    private Credit_Subsystem credit;

    public bool IsEligible(Customer cust)
    {
        bool isEligible = true;

        bank = new Bank_Subsystem();
        if (!bank.IsValidCustomer(cust))
        {
            isEligible = false;
        }

        loan = new Loan_Subsystem();
        if (isEligible && !loan.HasNoBadLoan(cust))
        {
            isEligible = false;
        }

        credit = new Credit_Subsystem();
        if (isEligible && !credit.HasGoodCredit(cust))
        {
            isEligible = false;
        }

        return isEligible;
    }
}

Note how the relationship between the entities has evolved. The customer still does not know about the subsystems and vice versa. It's the Facade that continues to do the magic.

Sample Image

Using the Code

As described earlier, use this Design Pattern when an application needs to be extensible and new requirements keep emerging. This will find its usage in a plethora of applications encompassing different industries.

Points of Interest

Facade is the most simple (arguably after Singleton) design pattern and makes programming beautiful and useful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I have more than 5 yrs of work ex as a software developer for a Financial Consulting firm and an Investment bank. My technical expertise include C# programming,WPF,WCF, SSRS reporting,ASP.Net and MS SQL (2005 & 2008).

Comments and Discussions

 
GeneralMy vote of 4 Pin
artes_x29-Oct-12 12:08
artes_x29-Oct-12 12:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.