· 1 min learn
This time let’s speak concerning the easy manufacturing unit design sample to encapsulate object creation in a extremely easy manner utilizing Swift.
Easy manufacturing unit implementation utilizing switch-case
The purpose of this sample is to encapsulate one thing that may typically differ. Think about a shade palette for an software. You may need to alter the colours in accordance with the newest behavior of the designer every day. I’d be actually inconvenient should you needed to search & change each single occasion of the colour code by hand. So let’s make a easy manufacturing unit in Swift that may return colours based mostly on a given type. 🎩
class ColorFactory {
enum Fashion {
case textual content
case background
}
func create(_ type: Fashion) -> UIColor {
swap type {
case .textual content:
return .black
case .background:
return .white
}
}
}
let manufacturing unit = ColorFactory()
let textColor = manufacturing unit.create(.textual content)
let backgroundColor = manufacturing unit.create(.background)
This may be actually helpful, particularly if it involves a sophisticated object initialization course of. You may as well outline a protocol and return numerous occasion sorts that implement the required interface utilizing a swap case block. 🚦
protocol Setting {
var identifier: String { get }
}
class DevEnvironment: Setting {
var identifier: String { return "dev" }
}
class LiveEnvironment: Setting {
var identifier: String { return "stay" }
}
class EnvironmentFactory {
enum EnvType {
case dev
case stay
}
func create(_ sort: EnvType) -> Setting {
swap sort {
case .dev:
return DevEnvironment()
case .stay:
return LiveEnvironment()
}
}
}
let manufacturing unit = EnvironmentFactory()
let dev = manufacturing unit.create(.dev)
print(dev.identifier)
So, a couple of issues to recollect concerning the easy manufacturing unit design sample:
+ it helps free coupling by separating init & utilization logic 🤔
+ it is only a wrapper to encapsulate issues that may change typically 🤷♂️
+ easy manufacturing unit will be carried out in Swift utilizing an enum and a switch-case
+ use a protocol if you're planning to return completely different objects (POP 🎉)
+ maintain it easy 🏭
This sample separates the creation from the precise utilization and strikes the accountability to a particular function, so if one thing adjustments you solely have to switch the manufacturing unit. You may go away all of your exams and every little thing else utterly untouched. Highly effective and easy! 💪
Associated posts
On this article I’m going to indicate you methods to implement a primary occasion processing system on your modular Swift software.
Be taught the iterator design sample by utilizing some customized sequences, conforming to the IteratorProtocol from the Swift normal library.
Discover ways 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. Discover ways to use lenses and prisms to govern objects utilizing a practical method.