Swift summary manufacturing unit design sample



· 1 min learn


Let’s mix manufacturing unit technique with easy manufacturing unit voilá: right here is the summary manufacturing unit design sample written in Swift language!

Summary manufacturing unit in Swift

The summary manufacturing unit sample gives a option to encapsulate a gaggle of particular person factories which have a typical theme with out specifying their concrete courses.

So summary manufacturing unit is there so that you can create households of associated objects. The implementation normally combines easy manufacturing unit & manufacturing unit technique ideas. Particular person objects are created via manufacturing unit strategies, whereas the entire thing is wrapped in an “summary” easy manufacturing unit. Now verify the code! 😅

// service protocols
protocol ServiceFactory {
    func create() -> Service
}

protocol Service {
    var url: URL { get }
}

// staging
class StagingService: Service {
    var url: URL { return URL(string: "https://dev.localhost/")! }
}

class StagingServiceFactory: ServiceFactory {
    func create() -> Service {
        return StagingService()
    }
}

// manufacturing
class ProductionService: Service {
    var url: URL { return URL(string: "https://stay.localhost/")! }
}

class ProductionServiceFactory: ServiceFactory {
    func create() -> Service {
        return ProductionService()
    }
}

// summary manufacturing unit
class AppServiceFactory: ServiceFactory {

    enum Surroundings {
        case manufacturing
        case staging
    }

    var env: Surroundings

    init(env: Surroundings) {
        self.env = env
    }

    func create() -> Service {
        swap self.env {
        case .manufacturing:
            return ProductionServiceFactory().create()
        case .staging:
            return StagingServiceFactory().create()
        }
    }
}

let manufacturing unit = AppServiceFactory(env: .manufacturing)
let service = manufacturing unit.create()
print(service.url)

As you possibly can see utilizing an summary manufacturing unit will affect the entire software logic, whereas manufacturing unit strategies have results solely on native components. Implementation can differ for instance you could possibly additionally create a standalone protocol for the summary manufacturing unit, however on this instance I wished to maintain issues so simple as I might.

Summary factories are sometimes used to attain object independence. For instance when you have a number of totally different SQL database connectors (PostgreSQL, MySQL, and so forth.) written in Swift with a typical interface, you could possibly simply swap between them anytime utilizing this sample. Identical logic may very well be utilized for something with the same state of affairs. 🤔

Associated posts


· 5 min learn


On this article I’m going to point out you easy methods to implement a primary occasion processing system on your modular Swift software.


· 4 min learn


Be taught the iterator design sample by utilizing some customized sequences, conforming to the IteratorProtocol from the Swift commonplace library.


· 4 min learn


Learn to use lazy properties in Swift to enhance efficiency, keep away from optionals or simply to make the init course of extra clear.


· 5 min learn


Newbie’s information about optics in Swift. Learn to use lenses and prisms to control objects utilizing a practical method.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles