The Façade Design Pattern in Swift
Difficulty: Beginner | Easy | Normal | Challenging
The supporting video
Missing the wonderful diagrams below, but this video might be of interest to you!
You will be expected to be aware how to make a Single View Application
You might like to see an example of the Façade Pattern from This article
Design Pattern: a general, reusable solution to a commonly occurring problem
The Façade Design Pattern: An object that masks complex or underlying (perhaps containing complex or multiple classes) classes
The Façade Pattern in Swift
If you wish to obfuscate complex code it can provide an interface that masks that complexity. In other words, the façade design pattern can make your code better (and in particular it can make your code easier to read).
In the real world we see the word façade used in architecture, and on a film set many of the buildings are a façade as there isn’t a real building. In other building projects a façade of an older building which hides the modern building underneath. An example of this is shown in the following image:
This gives the common idea of a façade hiding complexity under an outer shell.
Any façade can provide an interface to a complex subsystem, and when you interact with that object you will use the façade to interact with the same.
This gives us the advantages:
* Improve the readability and usability of software by masking interaction with complex components
* Enable a context-specific interface to functionality
* Enable decoupling of subsystems from clients and other subsystems
Here is a rather attractive diagram that shows the façade pattern:
Use of the façade pattern can help our coding by providing a simple view of the subsystem to the client.
The Simple Abstract Example
This is an abstract example, but don’t worry it is written in Swift.
Imagine we want to create an abstract model of a computer (you know, like in the Wikipedia article).
So we are going to create a computer facade that ultimately gives simple access to the constituent parts of a complex subsystem (The CPU, Memory and Drive).
Although don’t expect that code to do too much — but this is the type of example that you could use as a basis to solve your own problems! If you are going to do something like this, though, you should consider using Protocols and coding to those (Drive conforming to a protocol to enable Dependency Injection. If you wanted to make the Computer itself a protocol (not a class) be aware that a façade in an object rather than simply a protocol in Swift, which would make code taking this approach *not* conform to the façade pattern.
A coded example
Now this code obfuscates the complexity of downloading the image and setting it to the current image (is it is settled in an extension). This is not the traditional use of a façade pattern, but can loosely be said to follow the design pattern and hide the complexity of the operations which would happen in the
This isn’t necessarily the easiest concept that you will need to understand as a Swift programmer, but neither it is it the most complicated. You will need to use the Façade Design Pattern if you wish to provide a simple interface to a more complex problem. This allows consumers to avoid the complexity of interacting with complex classes (and potentially multiple classes) within a system.
If you’ve any questions, comments or suggestions please hit me up on Twitter