Objective ๐ฏ
Hide the complexity of the subsystems by providing a simplified interface to the client.
Type โ
โBehavioral: Describes how objects interact/communicate between themselves.
โCreational: Describes how to instantiate an object without large and complex.
โ๏ธStructural: Describes how objects/classes are composed to form larger structures.
UML ๐
Participants ๐
โข Facade:
- Provides simplified methods to be executed by subsystem objects
- Knows which subsystem classes are responsible for a request
โข Subsystem Classes:
- Execute some specific funcionality
- Have no knowledge of the facade and keep no reference to it
Sample Code ๐ฎ
Structural Example ๐๏ธ
public static class FacadeStructural
{
public static void Execute()
{
Facade lFacade = new Facade();
lFacade.MethodA();
lFacade.MethodB();
}
}
public class Facade
{
private SubSystemOne _SubSystemOne;
private SubSystemTwo _SubSystemTwo;
private SubSystemThree _SubSystemThree;
private SubSystemFour _SubSystemFour;
public Facade()
{
_SubSystemOne = new SubSystemOne();
_SubSystemTwo = new SubSystemTwo();
_SubSystemThree = new SubSystemThree();
_SubSystemFour = new SubSystemFour();
}
public void MethodA()
{
Console.WriteLine("Facade - Executing Method A");
_SubSystemOne.MethodOne();
_SubSystemFour.MethodFour();
}
public void MethodB()
{
Console.WriteLine("Facade - Executing Method B");
_SubSystemTwo.MethodTwo();
_SubSystemThree.MethodThree();
}
}
public class SubSystemOne
{
public void MethodOne()
{
Console.WriteLine("SubSystemOne - Executing Method One");
}
}
public class SubSystemTwo
{
public void MethodTwo()
{
Console.WriteLine("SubSystemTwo - Executing Method Two");
}
}
public class SubSystemThree
{
public void MethodThree()
{
Console.WriteLine("SubSystemThree - Executing Method Three");
}
}
public class SubSystemFour
{
public void MethodFour()
{
Console.WriteLine("SubSystemFour - Executing Method Four");
}
}
Output
Real-world Example ๐ฅ
public static class FacadePractical
{
public static void Execute()
{
OrderFacade lOrderFacade = new OrderFacade();
lOrderFacade.OrderFood();
}
}
public class OrderFacade
{
private Waiter _Waiter;
private Kitchen _Kitchen;
public OrderFacade()
{
_Waiter = new Waiter();
_Kitchen = new Kitchen();
}
public void OrderFood()
{
_Waiter.WriteOrder();
_Waiter.SendToKitchen();
_Kitchen.PrepareFood();
_Kitchen.CallWaiter();
_Waiter.ServerCustomer();
_Kitchen.WashDishes();
}
}
public class Waiter
{
public void WriteOrder()
{
Console.WriteLine("Waiter - Writting Order...");
}
public void SendToKitchen()
{
Console.WriteLine("Waiter - Sending To Kitchen...");
}
public void ServerCustomer()
{
Console.WriteLine("Waiter - Serving Customer...");
}
}
public class Kitchen
{
public void PrepareFood()
{
Console.WriteLine("Kitchen - Preparing Food...");
}
public void CallWaiter()
{
Console.WriteLine("Kitchen - Calling Waiter...");
}
public void WashDishes()
{
Console.WriteLine("Kitchen - Washing Dishes...");
}
}
Output
Source Code ๐ฒ
ย