Monday, October 23, 2023
HomeiOS DevelopmentSwift summary manufacturing facility design sample

Swift summary manufacturing facility design sample


Summary manufacturing facility in Swift

The summary manufacturing facility sample offers a solution 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 often combines easy manufacturing facility & manufacturing facility methodology rules. Particular person objects are created by manufacturing facility strategies, whereas the entire thing is wrapped in an “summary” easy manufacturing facility. Now test the code! 😅


protocol ServiceFactory {
    func create() -> Service
}

protocol Service {
    var url: URL { get }
}


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

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


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

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


class AppServiceFactory: ServiceFactory {

    enum Setting {
        case manufacturing
        case staging
    }

    var env: Setting

    init(env: Setting) {
        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 possibly can see utilizing an summary manufacturing facility will affect the entire software logic, whereas manufacturing facility strategies have results solely on native elements. Implementation can range for instance you might 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 if in case you have a number of completely different SQL database connectors (PostgreSQL, MySQL, and so on.) written in Swift with a typical interface, you might simply swap between them anytime utilizing this sample. Identical logic might be utilized for something with the same situation. 🤔



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments