Javatpoint Logo
Javatpoint Logo

Facade Design Pattern in C# with Examples

What is the Facade Design Pattern in C#?

The Facade Design Pattern specifies that you must offer a single interface to a group of interfaces within a subsystem, according to the GOF definition. The subsystem is made easier to use by the higher-level interface defined by the Facade Design Pattern.

A structural design called the Facade Design design offers a condensed user interface for a complicated system of classes, libraries, or frameworks. Presenting a minimal, unambiguous interface to external customers while assigning all of the intricate underlying tasks to the relevant classes within the system is the main objective of the facade pattern. Overlaying a collection of subsystems, the Facade class-typically a wrapper-enables cohesive communication between them.

Facade refers to the building's face, as the name would imply. Let's say you only made one structure. The only parts of the structure visible to anyone strolling outside are its glass and walls. The building's interior design, plumbing, electrical, and other intricate details have yet to be discovered by the people. In other words, the facade presents a welcoming front to individuals strolling about the building while concealing all of its intricate details.

Recognizing the C# Facade Design Pattern with a Single Real-Time Example

Determine Difficult Subsystems: Start by determining which intricate components of your system require simplification. These might be intricate systems or libraries with several interdependent classes.

Make a Facade Class: Construct a facade class that offers a straightforward interface to the intricate subsystems.

Assign Calls to Subsystems: The client requests should be assigned by the fa?ade to the relevant subsystem objects. All of the subsystems' complexities and dependencies ought to be managed by the fa?ade.

Client Code Interaction: By using the facade to communicate with the system, the client can more easily use the intricate subsystems.

Let's examine one Real-Time Example to grasp the C# Facade Design Pattern better.

To help you understand, please take a look at the accompanying diagram. In this case, we must create an application for online ordering using an e-commerce platform.

Facade Design Pattern in C# with Examples

As seen in the above image, the client must first create an object of the Product class and call the GetProductDetails function to obtain the product details in order to make an order.

Then, you must make the Payment if everything is in order (that is if the Product is in stock). The Client must call the MakePayment function and create an instance of the Payment class in order to accomplish this. The client must build an instance of the invoice class and use the SendInvoice function in order for us to send the invoice to the customer if the payment is successful. Therefore, the Client must complete the steps above in a specific order in order to place the order.

In C#, the Facade Design Pattern consists of an additional class that sits on top of the other classes. It is sometimes referred to as a Wrapper class or Facade Class.

Facade Design Pattern in C# with Examples

Thus, in this case, the extra class order is simply the Facade class's order placement responsibility. This class runs the methods in a specific order and internally produces an instance of each class. In order to place an order, the client will now call the Order Class, PlaceOrder method rather than the corresponding classes and their methods. The Product, Payment, and Invoice classes will be used internally by the PlaceOrder function to place the order.

NOTE: It's important to keep in mind that the Facade Design Pattern helps to relocate undesired dependencies to one location while also reducing the overall complexity of the program.

Understanding the Class or UML Diagram of Facade Design Pattern:

Let's examine the many elements of the Facade Design Pattern and the class diagram (also known as the UML diagram). To comprehend the class diagram for the Facade Design Pattern, please refer to the following graphic.

Facade Design Pattern in C# with Examples

The Facade Design Pattern involves three classes, as the graphic above illustrates. They are listed in the following order:

  • The Facade Class forwards client requests to the relevant subsystem objects after determining which subsystem classes are in charge of a particular request.
  • The Subsystem Classes carry out the corresponding functions that have been delegated to them; they are unaware of the Facade class.

NOTE: Despite not being aware of it, we have unintentionally applied the Facade Design Pattern extensively in our projects. You are utilizing Facade Design Patterns if you are working on Web APIs. When it comes to real-time applications, this is one of the best design patterns. Understanding the Facade Design Pattern will help you create a better project architecture.

Implementing Facade Design Pattern in C#:

Let's use C# to implement the example we talked about step-by-step.

Step 1: CreatingSubsystems

The Product, Payment, and Invoice classes are the Subsystem classes in our example; each class will be responsible for its tasks. So, let's establish the three classes mentioned above and assign responsibilities.

Payment Subsystem: After you confirm that the product is available, you must make the payment. The Payment Subsystem will be used in this process. Hence, copy and paste the following code into a class file called Payment. cs. There is a way to make the payment in this class. Here, you must verify the customer's method of payment. Using Net Banking, UPI, or Cash on Delivery, you must make the appropriate payment.

Invoice Subsystem: After a successful payment, we must give the customer a payment invoice. Thus, copy and paste the following code into a class file called Invoice. cs. To send the invoice, use the Sendinvoice function of this class. We occasionally send the client the Payment Failed Invoice as well. The request from the Facade class or the client directly may be accepted by the subsequent Invoice class.

NOTE: Other than printing the specifics, we have yet to really implement the techniques in this case. Our goal is to comprehend the C# implementation of the Facade Design Pattern rather than concentrate on the actual method implementations.

Step 2: Creating the Facade Class

It functions similarly to a wrapper class. So, copy and paste the following code into a class file called Order.cs. One method in this class will create objects of the subclass and call the corresponding methods in a specific order to place an order. The client can place an order using the straightforward interface provided by the following class.

With the aid of the Facade class, we are now concealing the intricacy involved in generating the many subclass instances and invoking their corresponding methods. Thus, this class serves as the subclasses' wrapper. The PlaceOrder method entirely handles placing an order.

Step 3: Client

The client is the only class that will utilize the Facade class. It will be the Programme class's Main method in our example. Thus, kindly make the following modifications to the Programme class's Main method.

Advantages of the Facade Design Pattern

  • Simplified Interface: Provides a single, more user-friendly interface for the intricate subsystems, hence simplifying their use.
  • Isolation: A certain amount of isolation from the intricate subsystems is provided by isolation, which is advantageous in situations where subsystem changes often.
  • Better Testability and Maintainability: By concentrating on system interfaces and reducing interdependencies, Facade can streamline the testing process.

Real-Time Applications:

The following situations are where the Facade Design Pattern comes in handy:

  • Simplifying Complex Systems: You should offer a straightforward interface to complex systems or frameworks that consist of several interconnected classes or layers. The Facade design can conceal this intricacy beneath a uniform, basic interface.
  • Decoupling Systems: By concealing the underlying intricacies of the client, a facade can offer loose coupling in a system where the components are closely coupled or interdependent.
  • Layered Architecture: Multi-layered architecture can have a facade that serves as the entrance to every stratum.
  • Enhanced Readability and Usability: When a system's readability and usability are your goals. It can be simpler for other developers to comprehend and utilize a complex subsystem when it has a clear and uncomplicated interface, such as a facade.
  • Reducing Dependencies: This approach lessens the reliance of external code on a library's or framework's internal operations, protecting the client code from upcoming modifications or subsystem complexity.
  • Subsystem Interface Standardisation: Harmonize subsystem interfaces. If each subsystem has distinct interfaces, a fa?ade can provide a consistent interface, making working with them easier.

Implementing the facade pattern can improve your program's encapsulation, maintainability, and flexibility. It encourages loose coupling, which makes it simpler to expand or change the subsystem later without negatively affecting the clients. Overall, the Facade pattern is a useful tool for controlling complexity and enhancing the general architecture of object-oriented programming languages, such as C#.

Conclusion:

In conclusion, by offering a single interface, the C# Facade design pattern offers a practical means of streamlining communication with a complicated subsystem. Clients can communicate with the subsystem through a set of clearly defined methods without having to comprehend its internal workings because the subsystem's complexity is contained behind a single facade class.


Next TopicImmutable in C#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news