· 1 min learn
This time let’s discuss 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 purpose of this sample is to encapsulate one thing that may typically fluctuate. Think about a shade palette for an software. You may need to vary the colours in line with the newest behavior of the designer each day. I’d be actually inconvenient if you happen to needed to search & substitute each single occasion of the colour code by hand. So let’s make a easy manufacturing facility in Swift that may return colours based mostly on a given fashion. 🎩
class ColorFactory {
enum Type {
case textual content
case background
}
func create(_ fashion: Type) -> UIColor {
change fashion {
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. You can even outline a protocol and return numerous occasion varieties that implement the required interface utilizing a change case block. 🚦
protocol Atmosphere {
var identifier: String { get }
}
class DevEnvironment: Atmosphere {
var identifier: String { return "dev" }
}
class LiveEnvironment: Atmosphere {
var identifier: String { return "reside" }
}
class EnvironmentFactory {
enum EnvType {
case dev
case reside
}
func create(_ sort: EnvType) -> Atmosphere {
change sort {
case .dev:
return DevEnvironment()
case .reside:
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 might be carried out in Swift utilizing an enum and a switch-case
+ use a protocol if you're planning to return totally different objects (POP 🎉)
+ preserve it easy 🏭
This sample separates the creation from the precise utilization and strikes the duty to a selected function, so if one thing adjustments you solely have to switch the manufacturing facility. You possibly can go away all of your exams and every thing else fully untouched. Highly effective and easy! 💪
Associated posts
· 2 min learn
Study concerning the initialization strategy of the 2 well-known lessons in UIKit. Say howdy to UIViewcontroller, and UIView init patterns.
· 4 min learn
Study the iterator design sample by utilizing 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 technique with easy manufacturing facility voilá: right here is the summary manufacturing facility design sample written in Swift language!