In the past two years, we’ve built more than 20 fully functional iOS Templates. As you can image, code reusability is one of the top priorities for us, when it comes to creating a Swift app. In order to reuse as much code as possible, we are focusing on modularizing our codebase as much as possible, by leveraging various design patterns, compatible with Swift. In this Swift tutorial, we are taking a look at the Facade Design Pattern. Let’s understand what it is, what kind of architectural approach it takes and let’s also see a code snippet implementing it in Swift.facade design pattern

The facade pattern (also spelled façade) is a software-design pattern commonly used in object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a front-facing interface masking more complex underlying or structural code. You can read up the official generic Facade definition on Wikipedia.

Let’s consider a concrete example, familiar to all iOS developers out there. When developing mobile apps, almost every time we are using URL communication to load/store resources from a remote server. In our case, the most common scenarios would be a Firebase instance or a WooCommerce/WordPress backend.

If you ever created a simple APIClient / APIManager class, you’ve used the Facade Design Pattern. Creating a simple APIClient/APIManager object that’s encapsulating the networking, is indeed a facade to the Alamofire or URLSession functionality. In this way, you create a facade that has a simple and handy API for quite complicated URL requests related logic.

Facade Design Pattern Example

If you’ve ever worked with the KeyChain, you know that Apple’s APIs are extremely cumbersome. In fact, almost no one used those APIs. Most of the iOS developers we know usually use a third party open source Swift project that wraps the logic of the KeyChain, hiding all those hideous interfaces. These Swift libraries are facades themselves.

For a simple facade example, let’s build a simple facade object for the storing values in Keychain:

class KeychainFacade {
    
    func save(value: String, forKey key: String) throws {
        // save to Keychain logic
    }
    
    func readValue(forKey key: String) -> throws String {
        // read value from Keychain logic
    }
}

As you can see, we haven’t added the implementation because this article is not about the Keychain, but you should get the idea.

Conclusion

The facade design pattern is widely spread in the industry, and chances are you’ve already used it, without realizing it. It is used to hide implementation details and abstract out interfaces so that consumers can deal with a lower level of details. While it adds an extra layer of abstraction, the facade design pattern improves the readability and architecture of the code significantly.

If you liked this article, please consider sharing it to help us spread the word. Happy Coding!

Categories: iOS Development

Leave a Reply

Your email address will not be published. Required fields are marked *