I believed that I might be good to have a summarized comparability between all of the manufacturing unit patterns, so right here it’s every thing that it’s best to find out about them. Setting up them is comparatively easy, on this instance I’ll use some UIColor
magic written within the Swift programming language to point out you the fundamentals. 🧙♂️
Static manufacturing unit
- no separate manufacturing unit class
- named static methodology to initialize objects
- can have cache & can return subtypes
extension UIColor {
static var main: UIColor { return .black }
static var secondary: UIColor { return .white }
}
let main = UIColor.main
let secondary = UIColor.secondary
Easy manufacturing unit
- one manufacturing unit class
- swap case objects within it
- encapsulates various code
- if record is simply too huge use manufacturing unit methodology as an alternative
class ColorFactory {
enum Model {
case main
case secondary
}
func create(_ fashion: Model) {
swap fashion
case .main:
return .black
case .secondary:
return .white
}
}
let manufacturing unit = ColorFactory()
let main = manufacturing unit.create(.main)
let secondary = manufacturing unit.create(.secondary)
Manufacturing unit methodology
- a number of (decoupled) manufacturing unit courses
- per-instance manufacturing unit methodology
- create a easy protocol for manufacturing unit
protocol ColorFactory {
func create() -> UIColor
}
class PrimaryColorFactory: ColorFactory {
func create() -> UIColor {
return .black
}
}
class SecondaryColorFactory: ColorFactory {
func create() -> UIColor {
return .white
}
}
let primaryColorFactory = PrimaryColorFactory()
let secondaryColorFactory = SecondaryColorFactory()
let main = primaryColorFactory.create()
let secondary = secondaryColorFactory.create()
Summary manufacturing unit
- combines easy manufacturing unit with manufacturing unit methodology
- has a worldwide impact on the entire app
protocol ColorFactory {
func create() -> UIColor
}
class PrimaryColorFactory: ColorFactory {
func create() -> UIColor {
return .black
}
}
class SecondaryColorFactory: ColorFactory {
func create() -> UIColor {
return .white
}
}
class AppColorFactory: ColorFactory {
enum Theme {
case darkish
case gentle
}
func create(_ theme: Theme) -> UIColor {
swap theme {
case .darkish:
return PrimaryColorFactory().create()
case .gentle:
return SecondaryColorFactory().create()
}
}
}
let manufacturing unit = AppColorFactory()
let primaryColor = manufacturing unit.create(.darkish)
let secondaryColor = manufacturing unit.create(.gentle)
So these are all of the manufacturing unit patterns utilizing sensible actual world examples written in Swift. I hope my sequence of articles will assist you to achieve a greater understanding. 👍