Be taught what is the distinction between static manufacturing facility, easy manufacturing facility, manufacturing facility technique 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 the whole lot that you need to find out about them. Establishing them is comparatively simple, on this instance I’ll use some UIColor
magic written within the Swift programming language to indicate you the fundamentals. 🧙♂️
- no separate manufacturing facility class
- named static technique to initialize objects
- can have cache & can return subtypes
extension UIColor
static var major: UIColor return .black
static var secondary: UIColor return .white
let major = UIColor.major
let secondary = UIColor.secondary
- one manufacturing facility class
- change case objects inside it
- encapsulates various code
- if listing is just too massive use manufacturing facility technique as an alternative
class ColorFactory
enum Model
case major
case secondary
func create(_ type: Model)
change type
case .major:
return .black
case .secondary:
return .white
let manufacturing facility = ColorFactory()
let major = manufacturing facility.create(.major)
let secondary = manufacturing facility.create(.secondary)
- a number of (decoupled) manufacturing facility courses
- per-instance manufacturing facility technique
- 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 major = primaryColorFactory.create()
let secondary = secondaryColorFactory.create()
- combines easy manufacturing facility with manufacturing facility technique
- has a world 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
change 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 collection of articles will provide help to to realize a greater understanding. 👍