Manufacturing facility methodology is only a non-static methodology
Let’s face it, this sample is only a methodology normally backed by easy protocols & courses. Begin with a extremely easy instance: think about a category that may create a base URL in your service endpoint. Let’s name it service manufacturing facility. 😅
class ServiceFactory {
func createProductionUrl() -> URL {
return URL(string: "https://localhost/")!
}
}
let manufacturing facility = ServiceFactory()
manufacturing facility.createProductionUrl()
You would possibly assume, that hey, this isn’t even near a manufacturing facility methodology sample, however await it… let’s make issues just a little bit sophisticated by making a protocol for the service class and a protocol for returning the url as effectively. Now we are able to implement our base manufacturing url protocol as a separate class and return that particular occasion from a manufacturing service manufacturing facility class. Simply test the code you will get it:
protocol ServiceFactory {
func create() -> Service
}
protocol Service {
var url: URL { get }
}
class ProductionService: Service {
var url: URL { return URL(string: "https://localhost/")! }
}
class ProductionServiceFactory: ServiceFactory {
func create() -> Service {
return ProductionService()
}
}
let manufacturing facility = ProductionServiceFactory()
let request = manufacturing facility.create()
Why did we separated all of the logic into two courses and protocols? Please imagine me decoupling is an effective factor. Any longer you can simply write a mocked service with a dummy url to mess around with. Clearly that’d want an identical manufacturing facility class.
These mock situations would additionally implement the service protocols so you can add new sorts in a comparatively painless method with out altering the unique codebase. The manufacturing facility methodology solves one particular drawback of a easy manufacturing facility sample. If the record – contained in the switch-case – turns into too lengthy, sustaining new objects will probably be hell with only one manufacturing facility. Manufacturing facility methodology solves this by introducing a number of manufacturing facility objects.