ยท 1 min learn
This time let’s discuss concerning the easy manufacturing unit design sample to encapsulate object creation in a extremely easy means 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 shade palette for an utility. You might need to alter the colours in response to the most recent behavior of the designer each day. Iโd be actually inconvenient should 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 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 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 change case block. ๐ฆ
protocol Atmosphere {
var identifier: String { get }
}
class DevEnvironment: Atmosphere {
var identifier: String { return "dev" }
}
class LiveEnvironment: Atmosphere {
var identifier: String { return "stay" }
}
class EnvironmentFactory {
enum EnvType {
case dev
case stay
}
func create(_ kind: EnvType) -> Atmosphere {
change kind {
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 unfastened coupling by separating init & utilization logic ๐ค
+ it is only a wrapper to encapsulate issues that may change usually ๐คทโโ๏ธ
+ easy manufacturing unit will be applied in Swift utilizing an enum and a switch-case
+ use a protocol if you're 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 modifications you solely have to switch the manufacturing unit. You possibly can go away all of your exams and every part else fully untouched. Highly effective and easy! ๐ช
Associated posts
On this article I’m going to indicate you methods to implement a fundamental occasion processing system on your modular Swift utility.
Study the iterator design sample through the use of some customized sequences, conforming to the IteratorProtocol from the Swift commonplace 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 control objects utilizing a practical strategy.

