top of page

Mastering Swift's Development Blog

Follow Us On Twitter
  • Writer's pictureJon Hoffman

Facade Pattern: Protocol Oriented Design Pattern

Updated: Sep 4, 2022

The façade pattern is a structural pattern which shows us how to provide a simplified universal interface to a more complex set of classes, library or framework. This simplified interface is designed to not only make the overall subsystem easier to use but to also ensure it is used correctly by encapsulating a lot of the necessary overhead required for using the system.

Problem We Are Trying To Solve

In our application or library, we have a complex set of types that are required to do a specific task. These types will need to be initialized, with their dependencies and may have a specific order of execution or logic involved with using them.

Our Solution

The solution that we will be presenting here is to create a type that will encapsulate the functionality that we wish to use into a simplified interface. This Façade type will be able to encapsulate the business logic thereby ensuring that it is coded correctly and not left up to other developers to implement.

The Example

In this simplified example we have a vacation planning site that lets you make reservations for your hotel, car rentals and flights all in one spot. It even allows you to make the reservations for multiple hotels, car rentals and flights at the same time.

As usual in our design pattern tutorials, we are going to be showing the minimal amount of code necessary to demonstrate this pattern because we want the focus of this post and how the command design pattern works and not on implementing the example.

In this example, like other posts in our protocol oriented design patterns, we will be showing the minimal about of code we think is necessary to demonstrate this pattern because we do not want the code to complicate what we are trying to demonstrate. With that in mind we will begin by defining a TravelItem protocol that our Flight, Hotel and CarRentaltypes will adopt. The following shows how these are defined.

protocol TravelItem {}

struct Flight: TravelItem {}
struct Hotel: TravelItem {}
struct CarRental: TravelItem {}

The Flight, Hotel and CarRental types will contain all the necessary information to make a reservation for their respective types like flight number, date(s) and company to make the reservation with. Next, we will define our TravelFacade type which will encapsulate the logic to make the reservations for the trip. The following code shows how we would do this.

struct TravelFacade {
    private var flights = [Flight]()
    private var carRentals = [CarRental]()
    private var hotels = [Hotel]()
    func addFlight(_ flight: Flight) {}
    func addCarRental(_ carRental: CarRental) {}
    func addHotel(_ hotel: Hotel) {}
    func makeReservations() {}

In the TravelFacade type we define three private properties which will contain arrays of the Flight, CarRental and Hotel types with three functions to add items to the arrays. What makes this pattern work is the makeReservations()function in the TravelFacade type. This function will encapsulate the necessary logic to make each reservation depending on the type of reservation (flight, car rental or hotel) and the company to make reservation through.

By encapsulating all the business logic to make reservation within our TravelFacade type, we present a very simplified interface in which to make our reservation. All we need to do is to add the flights, car rentals and hotels to the TravelFacade type and then call the makeReservation() function. We also avoid having the logic to make reservation spread throughout our code, it is all encapsulated in this one API, similar to the proxy design pattern.



Mastering Swift 5.3

The sixth edition of this bestselling book, updated to cover through version 5.3 of the Swift programming language




Protocol Oriented Programming

Embrace the Protocol-Oriented design paradigm, for better code maintainability and increased performance, with Swift.



bottom of page