· 1 min learn
Let’s mix manufacturing unit methodology 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 standard theme with out specifying their concrete lessons.
So summary manufacturing unit is there so that you can create households of associated objects. The implementation normally combines easy manufacturing unit & manufacturing unit methodology rules. Particular person objects are created by manufacturing unit strategies, whereas the entire thing is wrapped in an “summary” easy manufacturing unit. Now examine 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 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 may see utilizing an summary manufacturing unit will affect the entire software logic, whereas manufacturing unit strategies have results solely on native components. Implementation can range for instance you may 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 attain object independence. For instance you probably have a number of completely different SQL database connectors (PostgreSQL, MySQL, and so on.) written in Swift with a standard interface, you may simply swap between them anytime utilizing this sample. Similar logic could possibly be utilized for something with an analogous state of affairs. 🤔
Associated posts
On this article I’m going to indicate you how one can implement a fundamental occasion processing system on your modular Swift software.
Study the iterator design sample through the use of some customized sequences, conforming to the IteratorProtocol from the Swift normal library.
Learn to use lazy properties in Swift to enhance efficiency, keep away from optionals or simply to make the init course of extra clear.
Newbie’s information about optics in Swift. Learn to use lenses and prisms to control objects utilizing a practical strategy.