Design Pattern articles in this series are listed below:
Design Patterns - An Overview
Design Patterns – Using the Strategy Pattern in C#
Design Patterns – Using the Singleton Pattern in C#
Design Patterns – Using the Adapter Pattern in C#
Design Patterns - Using the Facade Pattern in C#
Design Patterns - Using the Composite Pattern in C#
Design Patterns - Using the Decorator Pattern in C#
Design Patterns - Using the Abstract Factory Pattern in C#
Design Patterns – Using the Builder Pattern in C#
Design Patterns - Using the Observer Pattern in C# (Events and Delegates)
Design Patterns – Using the Chain of Responsibility Pattern in C#
Design Patterns – Using the State Pattern in C#
Design Patterns – Which to use When? Take the Quiz!
What is a Facade Pattern?
The Facade pattern allows us to hide the implementation of subsystems by creating a simplified Facade class which provides high level views of the subsystems.
There are several benefits to break down a system into several logical components. It reduces dependency, is easier to maintain and helps in scalability. Once we have several subsystems each performing a logical unit of work, we can bring them together using the Facade pattern.
How is the Facade Pattern implemented?
Let’s assume we are working on an Online Course Registration System. When a user chooses to register for a course, we have to perform several tasks:
1. We have to ensure that there are available seats in the selected course (We have a RegisterCourse subsystem for performing this task)
2. We have to charge the student for the cost of tuition (We have a PaymentGateway subsystem for performing this task)
3. We have to then send a notification to the instructor informing them that a new student has registered (We have a NotifyUser subsystem for performing this task)
Let’s look at the code for the subsystems
namespace FacadePattern
{
/// <summary>
/// Subsystem for making financial transactions
/// </summary>
public class PaymentGateway
{
public bool ChargeStudent(string studentName, int costTuition)
{
//Charge the student
Console.WriteLine(String.Format("Charging student {0} for ${1}", studentName, costTuition.ToString()));
return true;
}
}
/// <summary>
/// Subsystem for registration of courses
/// </summary>
public class RegisterCourse
{
public bool CheckAvailability(string courseCode)
{
//Verify if the course is available..
Console.WriteLine(String.Format("Verifying availability of seats for the course : {0}", courseCode));
return true;
}
public int GetTuitionCost(string courseCode)
{
//Get the cost of tuition
return 1000;
}
}
/// <summary>
/// Subsystem for Notifying users
/// </summary>
public class NotifyUser
{
public bool Notify(string studentName)
{
//Get the name of the instructor based on Course Code
//Notify the instructor
Console.WriteLine("Notifying Instructor about new enrollment");
return true;
}
}
}
Let’s take a look at the Facade Class implementation
namespace FacadePattern
{
/// <summary>
/// The Facade class that simplifies executing methods in the subsystems and hides implementation for the client
/// </summary>
public class RegistrationFacade
{
private PaymentGateway _paymentGateWay;
private RegisterCourse _registerCourse;
private NotifyUser _notifyUser;
public RegistrationFacade()
{
_paymentGateWay = new PaymentGateway();
_registerCourse = new RegisterCourse();
_notifyUser = new NotifyUser();
}
public bool RegisterStudent(string courseCode, string studentName)
{
//Step 1: Verify if there are available seats
if (!_registerCourse.CheckAvailability(courseCode))
return false;
//Step 2: Charge the student for tuition
if (!_paymentGateWay.ChargeStudent(studentName, _registerCourse.GetTuitionCost(courseCode)))
return false;
//Step 3: If everything's successful so far, notify the instructor of the new registration
return _notifyUser.Notify(studentName);
}
}
}
The Facade class has instances of the subsystems and has functionality for Registering a student. The facade class checks course availability, charges the student and notifies the instructor.
The Client would look something like below.
namespace FacadePattern
{
class Program
{
static void Main(string[] args)
{
RegistrationFacade registrationFacade = new RegistrationFacade();
if (registrationFacade.RegisterStudent("DesignPatterns101", "Jane Doe"))
Console.WriteLine("Student Registration SUCCESSFUL!");
else
Console.WriteLine("Student Registration Unsuccessful");
}
}
}
The output would be
Physical Model - Class Diagram
Conclusion
Facades can be useful in different scenarios. Almost all large modern applications are made up of several subsystems that are independent of each other. This article illustrates an example of the Facade pattern in use. Do not forget to read other design pattern articles on this site. As always, you can subscribe to the RSS feed or follow me on Twitter to stay up to date.