Author - StudySection Post Views - 8 views
Facade Design

Facade Design Pattern In C#

The facade design pattern in C# is used to provide an interface to a framework, library or classes in a simplified way.
In developer language, this pattern hides the complexity of the code and logic and shows only the interface to the user.
This pattern gets implemented using one wrapper class with a set of methods available for the user. This pattern is particularly used when a system is very complex or difficult to understand and has multiple subsystems.

Let’s understand with an example

This is the interface specific to the burger.
public interface IBurger {
void GetVegBurger();
void GetNonVegBurger();
}

This is a burger provider class which will get burgers for their clients. Here methods can have other private methods which the client is not bothered about.
public class BurgerProvider: IBurger {
public void GetNonVegBurger() {
GetNonVegToppings();
Console.WriteLine("Getting Non Veg Burger.");
}
public void GetVegBurger() {
Console.WriteLine("Getting Veg Burger.");
}
private void GetNonVegToppings() {
Console.WriteLine("Getting Non Veg Burger Toppings.");
}
}

Similarly, this is the interface specific for the bread.
public interface IBread {
void GetGarlicBread();
void GetCheesyGarlicBread();
}

And this is a bread provider class.
public class BreadProvider: IBread {
public void GetGarlicBread() {
Console.WriteLine("Getting Garlic Bread.");
}
public void GetCheesyGarlicBread() {
GetCheese();
Console.WriteLine("Getting Cheesy Garlic Bread.");
}
private void GetCheese() {
Console.WriteLine("Getting Cheese.");
}
}

Below is the restaurant façade class, which will be used by the client to order different burgers or breads.
public class RestaurantFacade {
private IPizza _PizzaProvider;
private IBread _BreadProvider;
public RestaurantFacade() {
_PizzaProvider = new BurgerProvider();
_BreadProvider = new BreadProvider();
}
public void GetNonVegPizza() {
_PizzaProvider.GetNonVegBurger();
}
public void GetVegPizza() {
_PizzaProvider.GetVegBurger();
}
public void GetGarlicBread() {
_BreadProvider.GetGarlicBread();
}
public void GetCheesyGarlicBread() {
_BreadProvider.GetCheesyGarlicBread();
}
}

Finally, below is the main method of our program,
void Main() {
Console.WriteLine("----------------------CLIENT ORDERS FOR BURGERS----------------------------\n");
var facadeForClient = new RestaurantFacade();
facadeForClient.GetNonVegBurger();
facadeForClient.GetVegBurger();
Console.WriteLine("\n----------------------CLIENT ORDERS FOR BREAD----------------------------\n");
facadeForClient.GetGarlicBread();
facadeForClient.GetCheesyGarlicBread();
}

StudySection has a long list of certification exams that it offers through its online platform. The PHP Certification Exam is one of the programming certifications that it provides. Whether you are new to PHP programming or you have extensive experience in PHP programming, you can get a certification according to your level. Attach a PHP certification with your resume to get the most out of job offers.

Leave a Reply

Your email address will not be published.