Design Patterns with Swift: Facade – A Quick Guide

Design Patterns with Swift: Facade – A Quick Guide

Design patterns are used to solve common design problems and improve code readability. Facade is one such design pattern that is used to simplify the complex functionality of a system. It provides an interface to the existing system which is easy to use and understand. In this guide, we will learn about the Facade design pattern and how to implement it in Swift.

Facade is a structural design pattern where a class provides a simplified interface to the existing system. It hides the complexity of the system by providing a single, unified interface. The facade class provides a simple interface to the complex logic of the underlying system. It acts as a wrapper around the existing system and helps reduce the coupling between the client and the system.

Let’s take an example of an online store. It has several components like product catalogue, payment system, order management, etc. All these components are connected and work together to provide a seamless shopping experience to the user. Now, if the client wants to use any of these components, they have to interact with each component separately. This can be quite complex and confusing for the client.

This is where the Facade pattern comes into play. The Facade pattern provides a single interface to the client to access all the components of the system. This makes it easier for the client to use the system without having to worry about the complexity of the underlying system.

Now, let’s see how we can implement the Facade pattern in Swift. We will create a ShoppingFacade class which provides a single interface to the client to access all the components of the system.


class ShoppingFacade {
    private let productCatalog: ProductCatalog
    private let paymentSystem: PaymentSystem
    private let orderManagement: OrderManagement

    init(productCatalog: ProductCatalog, paymentSystem: PaymentSystem, orderManagement: OrderManagement) {
        self.productCatalog = productCatalog
        self.paymentSystem = paymentSystem
        self.orderManagement = orderManagement
    }

    func getProductDetails(productId: Int) -> String {
        return productCatalog.getProductDetails(productId: productId)
    }

    func calculateTotalPrice(products: [Int]) -> Double {
        return paymentSystem.calculateTotalPrice(products: products)
    }

    func placeOrder(products: [Int]) {
        orderManagement.placeOrder(products: products)
    }
}

class ProductCatalog {
    func getProductDetails(productId: Int) -> String {
        // Get product details from the product catalog
        return ""
    }
}

class PaymentSystem {
    func calculateTotalPrice(products: [Int]) -> Double {
        // Calculate the total price of the products
        return 0.0
    }
}

class OrderManagement {
    func placeOrder(products: [Int]) {
        // Place the order for the given products
    }
}

In the above code, we have created a ShoppingFacade class which provides a single interface to the client to access all the components of the system. The ShoppingFacade class has methods to get product details, calculate total price and place order. Each of these methods calls the corresponding method in the underlying system.

The ShoppingFacade class simplifies the complexity of the system by providing a single, unified interface. The client no longer has to worry about the complexity of the underlying system. They can simply use the ShoppingFacade class to access the functionality of the system.

Facade is a great way to simplify the complexity of a system. It provides a single interface to the client to access all the components of the system. This makes it easier for the client to use the system without having to worry about the complexity of the underlying system. In this guide, we have learned about the Facade design pattern and how to implement it in Swift.

Scroll to Top