Photo by Carlos Macías on Unsplash

Provide a simple interface to a complex set of classes

Swift — Problems Catalogue #7

Alex Ilovan
Published in
3 min readJul 29, 2022

--

Problem Definition:

Consider the following scenario. You have to implement an awesome Movie Theater App that controls the systems in charge of everything needed in order to experience the ultimate movie experience.

Now, in order to run a movie in a movie theatre, you need different things turned on, like the screen, the projector, the sound system and others but you don’t want to switch them on one by one. It’s tiresome and honestly quite boring. You want a simple interface where you only need to pass the movie and the entire theatre comes to life.

Problem Solution:

Solution — Facade it’s a structural design pattern that provides a simple interface to a complex set of classes.

Sounds fancy but honestly, it isn’t and you probably already used it without knowing it. 💪

Real-World Usage:

Let’s start by defining our Facade as a simple class that takes three objects: SoundSystem, Screen and Projector.

Also, let’s define a watch(movie: String) method that takes the movie and handles everything from turning on the screen to loading the disc that contains our movie.

This is the essence of the facade. A method that executes multiple methods in itself. ✨

Facade

Let’s also define the auxiliary classes that our Facade needs. The SoundSystem, Screen and Projector with the on() & off() methods.

Auxiliary Classes

Lastly, let’s instantiate and put them all together.

Putting everything together

Here is the beauty of Facade, we can just instantiate the MovieTheaterFacade and we just need to pass the movie and the entire system does everything by itself like turning on the projector, loading the movie and turning on the screen and sound system.

From this point on, the sky is the limit 🚀 well…almost.

Of course, this design pattern has its limitations but used in moderation, it’s a great tool in our development toolbox.

This is the seventh article in the Swift Problems Catalogue series in which I’ll tackle general software development problems. The aim is to have a quick reference guide that can be easily accessed when having a design/algorithm dillemma.

Let me know what you think and don’t be shy to share where and when this pattern simplified your coding experience 🎶

--

--

Alex Ilovan
salt&pepper

🚀Head of Mobile Development @S&P 💻Comp. Engineer 🪐Engineering Manager. You can visit at: https://www.linkedin.com/in/alex-ilovan-129161b4/