· 1 min learn
This time let’s discuss concerning the easy manufacturing facility design sample to encapsulate object creation in a extremely easy approach utilizing Swift.
Easy manufacturing facility implementation utilizing switch-case
The objective of this sample is to encapsulate one thing that may typically fluctuate. Think about a shade palette for an utility. You may need to vary the colours in keeping with the newest behavior of the designer each day. I’d be actually inconvenient in the event you needed to search & change each single occasion of the colour code by hand. So let’s make a easy manufacturing facility in Swift that may return colours primarily based on a given model. 🎩
class ColorFactory {
enum Model {
case textual content
case background
}
func create(_ model: Model) -> UIColor {
swap model {
case .textual content:
return .black
case .background:
return .white
}
}
}
let manufacturing facility = ColorFactory()
let textColor = manufacturing facility.create(.textual content)
let backgroundColor = manufacturing facility.create(.background)
This may be actually helpful, particularly if it involves an advanced object initialization course of. It’s also possible to 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 facility = EnvironmentFactory()
let dev = manufacturing facility.create(.dev)
print(dev.identifier)
So, a number of issues to recollect concerning the easy manufacturing facility design sample:
+ it helps unfastened coupling by separating init & utilization logic 🤔
+ it is only a wrapper to encapsulate issues that may change typically 🤷♂️
+ easy manufacturing facility will be applied in Swift utilizing an enum and a switch-case
+ use a protocol in case you are planning to return totally different objects (POP 🎉)
+ hold it easy 🏭
This sample separates the creation from the precise utilization and strikes the duty to a particular function, so if one thing modifications you solely have to switch the manufacturing facility. You’ll be able to go away all of your assessments and every part else utterly untouched. Highly effective and easy! 💪
Associated posts
· 5 min learn
On this article I’m going to indicate you learn how 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 control objects utilizing a practical method.