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 feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos