Take advantage of the facade design pattern to provide a simplified interface to a set of sub systems and hence reduce the dependencies and complexities in your designs Design patterns have evolved to provide solutions to recurring problems and complexities in software design. These are categorized into three distinct categories namely, Creational, Structural and Behavioural. Creational patterns are used to create and manage the mechanism of creating instances of classes. Structural patterns are used to realize the relationships among the entities. Behavioural design patterns deal with object collaboration and delegation of responsibilities. The facade design pattern is a creational design pattern, i.e., falls under the creational design pattern group and can be used to represent a gateway to a subsystem. In using the facade design pattern you can provide a simple and unified high level interface for a set of interfaces that make the sub systems decoupled from the client and also ensures that the sub systems are easy to use. When should I use the façade design pattern? You can use the facade pattern when you need to provide a simple interface as an entry point to access a complex system. In essence, if you have a complex system where the abstractions and the implementations of it are tightly coupled and you would not want the consumer to contact the complex system directly, the facade pattern is an excellent choice. In using the facade design pattern in this case, you can eliminate the need of the client to call the methods of the complex system directly and rather provide an interface to which the client can communicate. The facade pattern is also a good candidate if the sub system contains many different methods while you would need just a few of those methods from among them — you can use the facade design pattern in this case and provide only access to only those methods that are needed through a high level interface. Here’s what the Gang of Four has to say on the facade design pattern: “Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.” Implementing the Façade design pattern in C# We have covered the concepts — let’s now dig into some code. In this section we would discuss how we can implement the façade design pattern using C#. The participants in a typical implementation of the facade design pattern include the Facade and the Subsystem classes. Now, consider two subsystems named SubsystemA and SubsystemB. While the former is used to validate user credentials, the latter is used to validate a credit card number and pay an amount using the credit card. The following class named SubsystemA that contains a method called ValidateUser. class SubsystemA { public void ValidateUser(string userName, string password) { Console.WriteLine("Validate user credentials..."); } } The SubsystemB class is shown below – it contains two methods namely, ValidateCreditCard and PayAmount. class SubsystemB { public void ValidateCreditCard(string cardNumber) { Console.WriteLine("Validate credit card..."); } public void PayAmount(string cardNumber, double amount) { Console.WriteLine("Pay amount..."); } } And, here’s the Facade class. Note how the instances of the two sub systems we created earlier have been created inside the Facade class. There are two methods namely Operation1 and Operation2. While the former relates to validating the user credentials by taking advantage of the ValidateUser method of the SubsystemA class, the latter relates to validating the credit card number and then making a payment using the validated credit card. To validate the credit card number, the ValidateCreditCard method of SubsystemB class is called from within the Operation2 method. Next, the PayAmount method of the SubsystemB class is called to make a payment. public class Facade { SubsystemA firstSubsystem = new SubsystemA(); SubsystemB secondSubsystem = new SubsystemB(); public void Operation1(string userName, string password) { firstSubsystem.ValidateUser(userName, password); } public void Operation2(string cardNumber, double amount) { secondSubsystem.ValidateCreditCard("1234567890"); secondSubsystem.PayAmount(cardNumber, amount); } } OK, so what’s next? All we have to do now is instantiate the Facade class and then invoke the methods Operation1 and Operation2 respectively. class Program { static void Main(string[] args) { Facade facade = new Facade(); facade.Operation1("Joydip", "Joydip123"); facade.Operation2("1234567890", 100.00); Console.Read(); } } The sample program we implemented in this article illustrated how the internal intricacies and complexities are abstracted from the consumer when we use the façade design pattern. All that the client or the consumer needs to know is the methods of the façade that need to be called. Related content how-to How to use FastEndpoints in ASP.NET Core Take advantage of the free open-source FastEndpoints library to build fast and lean APIs in your ASP.NET Core applications. By Joydip Kanjilal Jul 11, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Refit to consume APIs in ASP.NET Core Take advantage of Refit REST library to simplify API consumption and make your code cleaner, more efficient, and easier to maintain. By Joydip Kanjilal Jul 04, 2024 10 mins C# Microsoft .NET Software Deployment how-to When to use an abstract class vs. interface in C# Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications. By Joydip Kanjilal Jun 20, 2024 10 mins Small and Medium Business Microsoft .NET C# how-to 6 security best practices for ASP.NET Core Learn the best practices and built-in safeguards for preventing attacks and protecting sensitive data in your ASP.NET Core web applications. By Joydip Kanjilal Jun 07, 2024 6 mins C# Microsoft .NET Web Development Resources Videos