6.8 C
New York
Friday, November 15, 2024

Evaluating manufacturing facility design patterns – The.Swift.Dev.



· 1 min learn


Study what is the distinction between static manufacturing facility, easy manufacturing facility, manufacturing facility methodology and summary manufacturing facility utilizing the Swift language.

I assumed that I’d be good to have a summarized comparability between all of the manufacturing facility patterns, so right here it’s every part that you must find out about them. Establishing them is comparatively easy, on this instance I’m going to make use of some UIColor magic written within the Swift programming language to point out you the fundamentals. 🧙‍♂️

Static manufacturing facility

  • no separate manufacturing facility class
  • named static methodology 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

Easy manufacturing facility

  • one manufacturing facility class
  • swap case objects within it
  • encapsulates various code
  • if record is simply too large use manufacturing facility methodology as a substitute
class ColorFactory {
    enum Fashion {
        case major
        case secondary
    }

    func create(_ fashion: Fashion) {
        swap fashion
        case .major:
            return .black
        case .secondary:
            return .white
    }
}
let manufacturing facility = ColorFactory()
let major = manufacturing facility.create(.major)
let secondary = manufacturing facility.create(.secondary)

Manufacturing facility methodology

  • a number of (decoupled) manufacturing facility courses
  • 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 major = primaryColorFactory.create()
let secondary = secondaryColorFactory.create()

Summary manufacturing facility

  • combines easy manufacturing facility with manufacturing facility methodology
  • has a worldwide impact on the entire app
// very same manufacturing facility methodology sample from above
protocol ColorFactory {
    func create() -> UIColor
}

class PrimaryColorFactory: ColorFactory {
    func create() -> UIColor {
        return .black
    }
}

class SecondaryColorFactory: ColorFactory {
    func create() -> UIColor {
        return .white
    }
}

// easy manufacturing facility sample from above utilizing the manufacturing facility strategies
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 provide help to to achieve a greater understanding. 👍

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles