[ad_1]
Manufacturing unit methodology is only a non-static methodology
Let’s face it, this sample is only a methodology normally backed by easy protocols & lessons. Begin with a extremely easy instance: think about a category that may create a base URL to your service endpoint. Let’s name it service manufacturing unit. 😅
class ServiceFactory {
func createProductionUrl() -> URL {
return URL(string: "https://localhost/")!
}
}
let manufacturing unit = ServiceFactory()
manufacturing unit.createProductionUrl()
You would possibly suppose, that hey, this isn’t even near a manufacturing unit methodology sample, however look forward to it… let’s make issues slightly bit sophisticated by making a protocol for the service class and a protocol for returning the url as nicely. Now we will implement our base manufacturing url protocol as a separate class and return that particular occasion from a manufacturing service manufacturing unit class. Simply verify the code you may 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 unit = ProductionServiceFactory()
let request = manufacturing unit.create()
Why did we separated all of the logic into two lessons and protocols? Please consider me decoupling is an effective factor. Any further you can simply write a mocked service with a dummy url to mess around with. Clearly that’d want an identical manufacturing unit class.
These mock cases 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 unit methodology solves one particular downside of a easy manufacturing unit sample. If the record – contained in the switch-case – turns into too lengthy, sustaining new objects shall be hell with only one manufacturing unit. Manufacturing unit methodology solves this by introducing a number of manufacturing unit objects.
[ad_2]
