[ad_1]
Be taught what is the distinction between static manufacturing facility, easy manufacturing facility, manufacturing facility methodology and summary manufacturing facility utilizing the Swift language.
Design patterns
I believed that I might be good to have a summarized comparability between all of the manufacturing facility patterns, so right here it’s all the things that you must learn about them. Developing them is comparatively easy, on this instance I will use some UIColor magic written within the Swift programming language to point out you the fundamentals. 🧙♂️
- no separate manufacturing facility 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
- one manufacturing facility class
- swap case objects within it
- encapsulates various code
- if checklist is simply too huge use manufacturing facility methodology as an alternative
class ColorFactory {
enum Model {
case main
case secondary
}
func create(_ type: Model) {
swap type
case .main:
return .black
case .secondary:
return .white
}
}
let manufacturing facility = ColorFactory()
let main = manufacturing facility.create(.main)
let secondary = manufacturing facility.create(.secondary)
- a number of (decoupled) manufacturing facility lessons
- per-instance manufacturing facility methodology
- create a easy protocol for manufacturing facility
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()
- combines easy manufacturing facility with manufacturing facility 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 facility = AppColorFactory()
let primaryColor = manufacturing facility.create(.darkish)
let secondaryColor = manufacturing facility.create(.gentle)
So these are all of the manufacturing facility patterns utilizing sensible actual world examples written in Swift. I hope my sequence of articles will make it easier to to achieve a greater understanding. 👍
[ad_2]
