HomeiOS DevelopmentSwift summary manufacturing unit design sample

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 offers a technique to encapsulate a gaggle of particular person factories which have a typical theme with out specifying their concrete lessons.

So summary manufacturing unit is there so that you can create households of associated objects. The implementation often combines easy manufacturing unit & manufacturing unit technique rules. Particular person objects are created by manufacturing unit strategies, whereas the entire thing is wrapped in an “summary” easy manufacturing unit. Now test 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://reside.localhost/")! }
}

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

// summary manufacturing unit
class AppServiceFactory: ServiceFactory {

    enum Atmosphere {
        case manufacturing
        case staging
    }

    var env: Atmosphere

    init(env: Atmosphere) {
        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 utility logic, whereas manufacturing unit strategies have results solely on native elements. Implementation can differ for instance you would additionally create a standalone protocol for the summary manufacturing unit, however on this instance I needed to maintain issues so simple as I may.

Summary factories are sometimes used to realize 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 would simply swap between them anytime utilizing this sample. Similar logic may very well be utilized for something with the same state of affairs. 🤔

Associated posts


· 2 min learn


Be taught in regards to the initialization strategy of the 2 well-known lessons in UIKit. Say howdy to UIViewcontroller, and UIView init patterns.


· 4 min learn


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


· 4 min learn


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


· 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!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments