Skip to main content

Design Patterns with Swift: Facade pattern


facade image

About the pattern

Facade pattern is one of the Structural Patterns. The main aim of it is to hide the complexity of a system, class or logic and provide all functionalities behind a simple interface. Commonly, Facade is implemented in a way that one class is related to other classes which represents a system logic. Please take a look at the diagram:
diagram
As you can see, there is one class called Facade which separates the logic from LogicA, LogicB, LogicC classes. As a result our client only call the Facade class in order to execute some methods that are implemented in other classes.

Implementation

Let’s imagine a simple scenario. You have already created a great app called Super-Photo. One of the core features of your app is saving/converting images with JPEG or PNG extension. In order to do this you want to save UIImage representation in two ways. One is saving it as a PNG type, the second is saving it as a JPEG file type.
Fistly, in order to handle our image types and possible errors in our code - it will be nice to have two enums that will make our code cleaner and more readable.
enum ImageSaverError: Error {
    case couldNotCreateDestinationPath
    case couldNotCreateJPEGDataFromImage
    case couldNotCreatePNGDataFromImage
    case couldNotSaveImageInDestinationPath
}

enum ImageType {
    case png
    case jpeg(compressionQuality: CGFloat)
}
On the next step you will need to create a class that will handle a data providing for each photo extension:
class ImageDataProvider {
    func data(from image: UIImage, type: ImageType) throws -> Data {
        switch type {
        case .jpeg(let compressionQuality):
            return try jpegData(from: image, compressionQuality: compressionQuality)
        case .png:
            return try pngData(from: image)
        }
    }

    private func pngData(from image: UIImage) throws -> Data {
        guard let imageData = UIImagePNGRepresentation(image) else { throw ImageSaverError.couldNotCreateJPEGDataFromImage }
        return imageData
    }

    private func jpegData(from image: UIImage, compressionQuality: CGFloat) throws -> Data {
        guard let imageData = UIImageJPEGRepresentation(image, compressionQuality) else { throw ImageSaverError.couldNotCreatePNGDataFromImage }
        return imageData
    }
}
As you’ve noticed, our ImageDataProvider takes image and type parameters and creates the image data with proper extension JPEG or PNG.
The last step is to create class which is needed to save UIImage. So let’s name it a PathProvider.
class PathProvider {
    func createDestinationPath(fileName: String) throws -> URL {
        guard let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
            throw ImageSaverError.couldNotCreateDestinationPath
        }
        let destinationPath = path.appendingPathComponent("\(fileName)")
        return destinationPath
    }
}
Ok, so at the moment we have two classes with some logic inside. Now it’s time to create a facade for it!
let's do this
Create a class called ImageSaverFacade :
class ImageSaverFacade {
    private let pathProvider = PathProvider()
    private let dataProvider = ImageDataProvider()

    func save(image: UIImage, type: ImageType, fileName: String, overwrite: Bool) throws {
        let destinationURL = try pathProvider.createDestinationPath(fileName: fileName)
        let imageData = try dataProvider.data(from: image, type: type)
        let writingOptions: Data.WritingOptions = overwrite ? (.atomic) : (.withoutOverwriting)
        try imageData.write(to: destinationURL, options: writingOptions)
    }
}
Our ImageSaverFacade class has two private objects of PathProvider and ImageDataProvider class. Because the client doesn’t need to know anything about logic inside, the only thing which ImageSaverFacade exposes to a public is one method:
  • func save(image: UIImage, type: ImageType, fileName: String, overwrite: Bool) throws
This method is the only thing that our client should care about.
Now let’s move on to the facade usage part:
let imageSaver = ImageSaverFacade()
let image = UIImage(named: "my_image")!
do {
    try imageSaver.save(image: image, type: .png, fileName: "my_file_name", overwrite: true)
} catch  {
    //handle Error
}
// or
do {
    try imageSaver.save(image: image, type: .jpeg(compressionQuality: 1.0), fileName: "my_file_name", overwrite: false)
} catch  {
    //handle Error
}

Conclusion

Please notice that our facade covers logic associated with Data providing and creating a valid URL for file destination. And because of that, it is super easy to save UIImage as PNG or JPEG using our ImageSaverFacade. Only thing to do is to pass the correct parameters to facade method.
Facade design pattern can be used in many cases. Facade creates for you a simple gateway to a complicated system. By using it you will definitely make your code simpler to understand and read.

Comments

Popular posts from this blog

Swift GCD part 1: Thread safe singletons

Preview Singletons are entities, referenced to the same instance of a class from everywhere in your code. It doesn't matter if you like them or not, you will definitely meet them, so it's better to understand how they work. Constructing and handling a set of data doesn't seem to be a big challenge at first glance. The problems appear when you try to optimise the user experience with background work and your app starts acting weird. ??‍♂️ After decades of watching your display mostly with a blank face, you finally realize that your data isn't handled consistently by the manager because you're accessing it (running tasks on it) from multiple threads at the same time. So you really do have to deal with making your singletons thread safe. This article series is dedicated to thread handling using Swift. In the first part below you will get a comprehensive insight into som...

Thread safe singleton’s in Swift

What are singletons? — Singleton is design patterns which says that there should be only one instance of the class for the lifetime of the application. One the best example of Singleton is AppDelegate . How to write a singleton class ? class DefaultDict{ private var dict:[String:Any] = [:] public static let sharedManager = DefaultDict() private init(){ } public func set(value:Any,key:String){ dict[key] = value } public func object(key:String) -> Any?{ dict[key] } public func reset(){ dict.removeAll() } }   Testing singleton class under concurrent circumstances. We are going to write an example where we will set values in dict from various threads and even try to access some with different threads. When we do this we will encounter a crash. If you look closely it will be because of race condition and the crash will be on line set(value:Any,key:String) . class ViewController: UIViewController { ...

Frame vs Bounds in iOS

This article is a repost of an answer I wrote on Stack Overflow . Short description frame = a view’s location and size using the parent view’s coordinate system ( important for placing the view in the parent) bounds = a view’s location and size using its own coordinate system (important for placing the view’s content or subviews within itself) Details To help me remember frame , I think of a picture frame on a wall . The picture frame is like the border of a view. I can hang the picture anywhere I want on the wall. In the same way, I can put a view anywhere I want inside a parent view (also called a superview). The parent view is like the wall. The origin of the coordinate system in iOS is the top left. We can put our view at the origin of the superview by setting the view frame’s x-y coordinates to (0, 0), which is like hanging our picture in the very top left corner of the wall. To move it right, increase x, to move it down increase y. To help me remember bound...