· 1 min learn
This time let’s speak concerning the easy manufacturing facility design sample to encapsulate object creation in a very easy approach utilizing Swift.
Easy manufacturing facility implementation utilizing switch-case
The aim of this sample is to encapsulate one thing that may usually fluctuate. Think about a colour palette for an utility. You may need to vary the colours in line with the most recent behavior of the designer each day. I’d be actually inconvenient for those who 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 type. 🎩
class ColorFactory {
enum Type {
case textual content
case background
}
func create(_ type: Type) -> UIColor {
swap type {
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 a sophisticated object initialization course of. It’s also possible to outline a protocol and return varied 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 "dwell" }
}
class EnvironmentFactory {
enum EnvType {
case dev
case dwell
}
func create(_ sort: EnvType) -> Setting {
swap sort {
case .dev:
return DevEnvironment()
case .dwell:
return LiveEnvironment()
}
}
}
let manufacturing facility = EnvironmentFactory()
let dev = manufacturing facility.create(.dev)
print(dev.identifier)
So, just a few 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 usually 🤷♂️
+ easy manufacturing facility might 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 🎉)
+ maintain it easy 🏭
This sample separates the creation from the precise utilization and strikes the accountability to a selected function, so if one thing modifications you solely have to switch the manufacturing facility. You possibly can go away all of your checks and every part else fully untouched. Highly effective and easy! 💪
Associated posts
· 2 min learn
Be taught concerning the initialization means of the 2 well-known lessons in UIKit. Say hi there to UIViewcontroller, and UIView init patterns.
· 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
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.
· 1 min learn
Let’s mix manufacturing facility methodology with easy manufacturing facility voilá: right here is the summary manufacturing facility design sample written in Swift language!