Swift World: Design Patterns - Simple Factory

If we want to learn a programming language, we need to live with it. It means to use Swift as much as possible. Today, we will start talking about design patterns in Swift. In addition to simple explanation, we also try to give figures, samples, use cases, etc. Let’s get started.

The first pattern is simple factory. Simply speaking, we build a factory to produce different objects which belong to the same type. As in the following figure and code, we have different types of cars, sedan, SUV, and maybe van. They all conform to protocol Car. It means the protocol defines common interface for cars. The specific car type implement their own logic.

《Swift World: Design Patterns - Simple Factory》

enum CarType {
    case sedan, SUV
}

protocol Car {
    func drive()
}

class Sedan: Car {
    func drive() {
        print("drive a sedan")
    }
}

class SUV: Car {
    func drive() {
        print("drive a SUV")
    }
}

let sedan = Sedan()
sedan.drive()
let suv = SUV()
suv.drive()

If we want to add van, we create van class.

enum CarType {
    case sedan, SUV, van
}
class Van: Car {
    func drive() {
        print("drive a van")
    }
}

Then the instance

let van = Van()
van.drive()

Let’s try simple factory pattern to do the same thing. We need a factory to do the job to produce different types of cars.

《Swift World: Design Patterns - Simple Factory》

class Factory {
    static func produceCar(type: CarType) -> Car {
        switch type {
        case .sedan:
            return Sedan()
        case .SUV:
            return SUV()
        }
    }
}

let sedan = Factory.produceCar(type: .sedan)
sedan.drive()
let suv = Factory.produceCar(type: .SUV)
suv.drive()

Obviously, it is the factory which creates different instances of cars. Then what if we want to add van? The van class have been defined. So here we need to change the factory to enable it to produce van.

class Factory {
    static func produceCar(type: CarType) -> Car {
        switch type {
        case .sedan:
            return Sedan()
        case .SUV:
            return SUV()
        case .van
            return Van()
        }
    }
}

let sedan = Factory.produceCar(type: .sedan)
sedan.drive()
let suv = Factory.produceCar(type: .SUV)
suv.drive()
let van = Factory.produceCar(type: .van)
van.drive()

That’s all simple factory pattern does. But why do we need this pattern? It separates the creation from usage and move the responsibility to a specific role. Or speak in professional terms, it help loose coupling. Let’s assume the initialization logic of sedan is changed one day. We don’t need to change every creation in the whole project. To change the logic in factory is all we need to do.

In next blog of this series, we will talk about Factory Method Pattern.

Thanks for your time.

Please enable JavaScript to view the <a href=”https://disqus.com/?ref_noscript”>comments powered by Disqus.</a>

    原文作者:算法小白
    原文地址: https://juejin.im/entry/58bf8cf661ff4b005d76f1ce
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞