Learn Design Pattern - Facade Pattern

In the previous article we discussed the Decorator Pattern. Today we will cover the Facade Pattern.

Agenda

  • What is Facade Pattern?
  • When to use Facade Pattern?
  • Implement a Facade Pattern in a .Net console-based application.
  • Class Diagram.

Previous Articles

  1. Design Patterns: Introduction
  2. Learn Design Pattern - Singleton Pattern
  3. Learn Design Pattern - Factory Method Pattern
  4. Learn Design Pattern - Abstract Factory Pattern
  5. Learn Design Pattern - Builder Pattern
  6. Learn Design Pattern - Prototype Pattern
  7. Learn Design Pattern - Adapter Pattern
  8. Learn Design Pattern - Composite Pattern
  9. Learn Design Pattern – Decorator Pattern

What is Facade Pattern

  • The GOF says the "Facade Pattern provides a unified interface to a set of interfaces in a subsystem.".
  • Facade defines a higher-level interface that makes the subsystem easier to use.

Don't go into the complexity of the definition, it's the simplest one and you might have used this pattern many times in your project.

Let's define it in simpler words; they are:

  • Using this pattern we make objects look like something they're not.
  • This pattern is called Facade because it hides all the complexity of one or more classes.

When to use?

Consider a customer applying for a bank loan. Now the bank has various departments like Address Verification Department, Finance Verification department and many more.

Our task is to write an application which will check whether the customer qualifies for a loan or not.

Our application will have the following classes:
Customer, Bank, AddressVerifier, SalaryVerifer.

The Client Code will end up like this:

Customer c = new Customer();

AddressVerifier objAddressVerifier = new AddressVerifier();

bool isAddressVerified=objAddressVerifier.HasProperAddress(c);

 

FinanceVerifier objFinanceVerifier = new FinanceVerifier();

bool hasGoodSalary = objFinanceVerifier.HasGoodSalary(c);

bool hasAnyOtherLoan = objFinanceVerifier.HaveAnyOtherLoan(c);

 

if(isAddressVerified && hasGoodSalary && !hasAnyOtherLoan)

{

          Console.WriteLine("Loan Approved");

}

else

{

          Console.WriteLine("Loan Rejected");

}

The Client code has become very complex.

Solution Facade Pattern

A Facade is just what you need: we will create a Facade class which will provide a simpler method which does the complex task for us.
 

public class LoanFascade

{

          public bool doApplicableForLoan(Customer c)

          {

                    AddressVerifier objAddressVerifier = new AddressVerifier();

                    bool isAddressVerified = objAddressVerifier.HasProperAddress(c);

 

                    FinanceVerifier objFinanceVerifier = new FinanceVerifier();

                    bool hasGoodSalary = objFinanceVerifier.HasGoodSalary(c);

                    bool hasAnyOtherLoan = objFinanceVerifier.HaveAnyOtherLoan(c);

 

                    return (isAddressVerified && hasGoodSalary && !hasAnyOtherLoan);

          }

}

Client Code
 

Customer c = new Customer();

LoanFascade objLoanFascade=new LoanFascade();

if(objLoanFascade.doApplicableForLoan(c))

{

          Console.WriteLine("Loan Approved");

}

else

{

          Console.WriteLine("Loan Rejected");

}


Class Diagram

Facade-pattern.jpg

That's it, it's a very simple pattern and hope all of you enjoyed it.

You can download the sample attached for a practical demonstration.


Feedback and suggestions are always welcome. 

 

Up Next
    Ebook Download
    View all
    Learn
    View all