What is a Façade Design Pattern?

Written by ssukhpinder | Published 2023/02/28
Tech Story Tags: web-development | design-patterns | csharp | software-development | dotnet | dotnet-core | programming | aspnetcore | web-monetization | hackernoon-es | hackernoon-hi | hackernoon-zh | hackernoon-vi | hackernoon-fr | hackernoon-pt | hackernoon-ja

TLDRA facade design pattern provides a single entry point to a subsystem, hiding its complexity from the client. In our example, we can create a facade that provides a unified interface to the inventory, payment, and shipping subsystems. The article demonstrates Facade design patterns using the C# programming language.via the TL;DR App

As applications grow in size and complexity, managing the interdependence between subsystems can become challenging. A facade design pattern simplifies this interaction and provides a unified interface to a set of interfaces in a subsystem.

Use Case

Consider an online shopping application that allows customers to browse products, add them to a cart, and checkout. The application has different subsystems responsible for managing other parts, such as the inventory, payment, and shipping systems. These subsystems have different interfaces and need to communicate with each other to complete a purchase.

The problem is that managing this communication between subsystems can be difficult as the application grows. Changes in one subsystem can have a cascading effect on other subsystems, leading to a tangled and hard-to-maintain codebase.

We can use the facade design pattern to simplify the interaction between subsystems. A facade pattern provides a single entry point to a subsystem, hiding its complexity from the client. In our example, we can create a facade that provides a unified interface to the inventory, payment, and shipping subsystems.

Prerequisites

  • Basic knowledge of OOPS concepts.
  • Any programming language knowledge.

The article demonstrates Facade design patterns using the C# programming language.

Learning Objectives

  • How to code using a Facade design pattern

Getting Started

Let’s start by defining the interfaces for our subsystems

public interface IInventorySystem
{
    void Update(int productId, int quantity);
    bool IsAvailable(int productId, int quantity);
}

public interface IPaymentSystem
{
    bool Charge(double amount);
}

public interface IShippingSystem
{
    bool Ship(string address);
}

Next, we can implement the subsystems:

public class InventorySystem : IInventorySystem
{
    public void Update(int productId, int quantity)
    {
        // update inventory
    }

    public bool IsAvailable(int productId, int quantity)
    {
        // check if inventory is available
        return true;
    }
}

public class PaymentSystem : IPaymentSystem
{
    public bool Charge(double amount)
    {
        // charge the customer
        return true;
    }
}

public class ShippingSystem : IShippingSystem
{
    public bool Ship(string address)
    {
        // ship the product
        return true;
    }
}

Finally, we can create a facade that provides a simple interface to these subsystems:

public class OrderFacade
{
    private IInventorySystem _inventorySystem;
    private IPaymentSystem _paymentSystem;
    private IShippingSystem _shippingSystem;

    public OrderFacade()
    {
        _inventorySystem = new InventorySystem();
        _paymentSystem = new PaymentSystem();
        _shippingSystem = new ShippingSystem();
    }

    public bool PlaceOrder(int productId, int quantity, double amount, string address)
    {
        bool success = true;

        if (_inventorySystem.IsAvailable(productId, quantity))
        {
            _inventorySystem.Update(productId, -quantity);
            success = success && _paymentSystem.Charge(amount);
            success = success && _shippingSystem.Ship(address);
        }
        else
        {
            success = false;
        }

        return success;
    }
}

In the OrderFacade class, we create instances of the subsystems and offer a simple method PlaceOrder that takes in the product id, quantity, amount, and shipping address. The PlaceOrder technique uses the subsystems to check the inventory, charge the customer, and ship the product.

With the facade pattern, the client code can create an instance of the OrderFacade class and call the PlaceOrder method without worrying about the details of the subsystems.

var order = new OrderFacade();
bool success;

// place an order
success = order.PlaceOrder(productId: 123, quantity: 1, amount: 99.99, address: "123 Main St");

if (success)
{
Console.WriteLine("Order placed successfully");
}
else
{
Console.WriteLine("Unable to place order");
}

In this example, we have used the facade pattern to simplify the interaction between subsystems in an online shopping application. The client code only needs to interact with the `OrderFacade` class and does not need to know about the inventory, payment, and shipping subsystems.

Conclusion

The facade design pattern simplifies the interaction between subsystems and provides a unified interface to a set of interfaces in a subsystem. It can help reduce coupling and improve the maintainability of large and complex applications. This article explores the facade design pattern in C# with a use case and code examples. Using the facade pattern, we can simplify the codebase and make it easier to maintain and extend.

Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section.

Follow me on

C# Publication, Dev. to, Pinterest, Substack, Hashnode, Write.as

More design patterns — Reference Linkedin Learning


Also publishedhere.


Written by ssukhpinder | I'm Sukhpinder Singh, a passionate self-taught .Net developer from India.
Published by HackerNoon on 2023/02/28