· 1 min learn
This time let’s discuss concerning the easy manufacturing unit design sample to encapsulate object creation in a extremely easy approach utilizing Swift.
Easy manufacturing unit implementation utilizing switch-case
The purpose of this sample is to encapsulate one thing that may usually fluctuate. Think about a coloration palette for an utility. You may need to alter the colours based on the most recent behavior of the designer every day. I’d be actually inconvenient when you needed to search & exchange 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 model. 🎩
class ColorFactory {
enum Type {
case textual content
case background
}
func create(_ model: Type) -> UIColor {
swap model {
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 varieties that implement the required interface utilizing a swap case block. 🚦
protocol Surroundings {
var identifier: String { get }
}
class DevEnvironment: Surroundings {
var identifier: String { return "dev" }
}
class LiveEnvironment: Surroundings {
var identifier: String { return "dwell" }
}
class EnvironmentFactory {
enum EnvType {
case dev
case dwell
}
func create(_ sort: EnvType) -> Surroundings {
swap sort {
case .dev:
return DevEnvironment()
case .dwell:
return LiveEnvironment()
}
}
}
let manufacturing unit = EnvironmentFactory()
let dev = manufacturing unit.create(.dev)
print(dev.identifier)
So, a number of issues to recollect concerning the easy manufacturing unit design sample:
+ it helps unfastened coupling by separating init & utilization logic 🤔
+ it is only a wrapper to encapsulate issues that may change usually 🤷♂️
+ easy manufacturing unit might be applied in Swift utilizing an enum and a switch-case
+ use a protocol in case you are planning to return completely different objects (POP 🎉)
+ hold it easy 🏭
This sample separates the creation from the precise utilization and strikes the duty to a selected position, so if one thing adjustments you solely have to change the manufacturing unit. You possibly can go away all of your exams and all the pieces else fully untouched. Highly effective and easy! 💪
Associated posts
· 5 min learn
On this article I’m going to point out you the way to implement a fundamental occasion processing system on your modular Swift utility.
· 4 min learn
Be taught the iterator design sample through the use of some customized sequences, conforming to the IteratorProtocol from the Swift normal library.
· 4 min learn
Learn to use lazy properties in Swift to enhance efficiency, keep away from optionals or simply to make the init course of extra clear.
· 5 min learn
Newbie’s information about optics in Swift. Learn to use lenses and prisms to govern objects utilizing a useful strategy.