Facade Design Pattern in Swift for Beginners (with async/await)

activesludge
AVIV Product & Tech Blog
4 min readJun 19, 2023

--

Imagine you are visiting a local restaurant. The first thing you think of, should never be whether the kitchen has enough potatoes or enough gas to fire the stove, or whether the chef has the ability to cook the dish you want or not.

That is when the waiters/waitresses come into play. They greet you with a smile, show you where you can sit, give you a simple menu, and even tell you their recommendations or what's not available. They are the facade between you and the complex internal operations in the restaurant.

Image by pch.vector on Freepik

Similarly, software development can get complicated with many different components to manage. The Facade design pattern simplifies the system and provides a straightforward interface for clients to use.

In this article, we will see how to implement the Facade pattern in Swift with a practical example.

Introduction

The Facade Design Pattern is a structural design pattern that provides a simplified interface to a complex system. That should sound familiar right? Since that's what we do in programming in general…

We gather independent components from classes, frameworks, and libraries, then provide a simple, meaningful, single answer.

Facade Design Pattern Diagram

It consists of three actors:

  • Consumer: Owns and uses the facade object.
  • Facade: Gathers various dependencies around and provides one meaningful method to interact with the system.
  • Dependency: An object that serves a specific purpose in its domain. Each dependency performs a small part of a complex task.

When do we use it?

Occasionally, it is necessary to obtain appropriate responses from various APIs before initiating any processes. If you find yourself in a position where you need to convert a bunch of responses to a more simple one, Facade Design Pattern is what you are looking for.

Instead of expecting consumers to know each component of the complex system, we can provide a facade to expose the system properly and safely.

Code Example

We are going to implement an imaginary feature where we order french fries from a waiter at our favorite restaurant. A set of different requirements must be met, in order, before eating them.

  1. There have to be enough potatoes
  2. Potatoes have to be prepared by the chef
  3. A frier will fry them
  4. The chef will serve them in a basket

Finally, the waiter will deliver them.

As we can see, these are quite independent components and actors. They each require different methods to get answers. They each require a different time to complete… It is cruel to expect a customer to know all these and tell the restaurant every time they need french fries, right? Plus, what if they want a burger instead of fries, should they know the steps as well? The answer is no.

Let's create a facade for the restaurant, and make the ordering of french fries available to any customer.

We will take advantage of the async/await feature of Swift as well. Our example will leverage this feature in a serial way. Since each step has to be completed before proceeding to the next step.

Open up an Xcode playground and start following through the code below.

We have created the Restaurant struct along with the necessary steps to cook a delicious french fry. Each one has its own responsibilities. For simplicity, I have used the Task.Sleep method to simulate the amount of work to be done. Also, all these functions are declared private since we don't want to disclose our facade's implementation to the user.

The async keyword at the end of a function allows the compiler to know that this function can be used in an asynchronous context. Either serially or concurrently.

Now let's gather all these internal steps and produce a public function.

Here we declared a public function to order a french fry. await keyword before each method call indicates that the program should wait for this method to finish before proceeding.

Now, let's see it in action!

The Task keyword here indicates that the closure inside is an asynchronous unit of work. It's required to use async/await context anywhere in the code.

If you run, you should see each step progressing and finally serving the french fries. 😋

Go ahead and experiment on the brand new facade of yours. 🔬🧪

Now, this is a very limited example of course, where we print simple text. In real life, what you would do is have your dependency methods able to throw errors, then the facade can catch them and handle each of them accordingly!

Challenge

Implement the error throwing for each dependency. Follow these steps:

  1. Create a FrenchFriesErrors. Add types of errors that each step can throw.
  2. Add throws ability to dependency methods. Plant an error in one of the steps.
  3. Capture all methods inside the facade in a do block.
  4. Handle each error in the catch block.

Try it yourself first.

Notice that the proceeding steps will not execute after a step throws an error.

Solution

Congratulations. You’ve made it to the end.

Now you know what to do if you have to create a complex interface with different dependencies.

I hope the above explanation makes sense and will be helpful to you on your iOS development journey.

Have a great one.

--

--