Friday, March 13, 2026
HomeiOS DevelopmentEvaluating manufacturing unit design patterns - The.Swift.Dev.

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



· 1 min learn


Be taught what is the distinction between static manufacturing unit, easy manufacturing unit, manufacturing unit technique and summary manufacturing unit utilizing the Swift language.

I believed that I’d be good to have a summarized comparability between all of the manufacturing unit patterns, so right here it’s all the pieces that you must learn 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 unit

  • no separate manufacturing unit 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

Easy manufacturing unit

  • one manufacturing unit class
  • swap case objects inside it
  • encapsulates various code
  • if record is just too large use manufacturing unit technique as an alternative
class ColorFactory {
    enum Model {
        case major
        case secondary
    }

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

Manufacturing facility technique

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

Summary manufacturing unit

  • combines easy manufacturing unit with manufacturing unit technique
  • has a worldwide impact on the entire app
// very same manufacturing unit technique 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 unit sample from above utilizing the manufacturing unit strategies
class AppColorFactory: ColorFactory {

    enum Theme {
        case darkish
        case mild
    }

    func create(_ theme: Theme) -> UIColor {
        swap theme {
        case .darkish:
            return PrimaryColorFactory().create()
        case .mild:
            return SecondaryColorFactory().create()
        }
    }
}

let manufacturing unit = AppColorFactory()
let primaryColor = manufacturing unit.create(.darkish)
let secondaryColor = manufacturing unit.create(.mild)

So these are all of the manufacturing unit patterns utilizing sensible actual world examples written in Swift. I hope my sequence of articles will show you how to to achieve a greater understanding. 👍

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments