Swift summary manufacturing facility design sample

Swift summary manufacturing facility design sample



· 1 min learn


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

Summary manufacturing facility in Swift

The summary manufacturing facility sample gives a strategy to encapsulate a gaggle of particular person factories which have a typical theme with out specifying their concrete lessons.

So summary manufacturing facility is there so that you can create households of associated objects. The implementation normally combines easy manufacturing facility & manufacturing facility methodology rules. Particular person objects are created by way of manufacturing facility strategies, whereas the entire thing is wrapped in an “summary” easy manufacturing facility. 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 facility
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 facility = AppServiceFactory(env: .manufacturing)
let service = manufacturing facility.create()
print(service.url)

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

Summary factories are sometimes used to attain object independence. For instance when you have a number of completely 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 might be utilized for something with an identical state of affairs. 🤔

Associated posts


· 5 min learn


On this article I’m going to indicate you how you can implement a fundamental occasion processing system in 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 customary 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.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.