Swift easy manufacturing facility design sample

0/5 No votes

Report this app



Easy manufacturing facility implementation utilizing switch-case

The aim of this sample is to encapsulate one thing that may typically fluctuate. Think about a colour palette for an software. You might need to vary the colours in line with the most recent behavior of the designer each day. I might be actually inconvenient for those who needed to search & substitute each single occasion of the colour code by hand. So let’s make a easy manufacturing facility in Swift that may return colours based mostly on a given model. ๐ŸŽฉ

class ColorFactory {

    enum Fashion {
        case textual content
        case background

    func create(_ model: Fashion) -> UIColor {
        change model {
        case .textual content:
            return .black
        case .background:
            return .white

let manufacturing facility = ColorFactory()
let textColor = manufacturing facility.create(.textual content)
let backgroundColor = manufacturing facility.create(.background)

This may be actually helpful, particularly if it involves a sophisticated object initialization course of. It’s also possible to outline a protocol and return numerous occasion varieties that implement the required interface utilizing a change case block. ๐Ÿšฆ

protocol Atmosphere {
    var identifier: String { get }

class DevEnvironment: Atmosphere {
    var identifier: String { return "dev" }

class LiveEnvironment: Atmosphere {
    var identifier: String { return "stay" }

class EnvironmentFactory {

    enum EnvType {
        case dev
        case stay

    func create(_ kind: EnvType) -> Atmosphere {
        change kind {
        case .dev:
            return DevEnvironment()
        case .stay:
            return LiveEnvironment()

let manufacturing facility = EnvironmentFactory()
let dev = manufacturing facility.create(.dev)

So, a number of issues to recollect in regards to the easy manufacturing facility design sample:

  • it helps unfastened coupling by separating init & utilization logic ๐Ÿค”
  • it is only a wrapper to encapsulate issues that may change typically ๐Ÿคทโ€โ™‚๏ธ
  • easy manufacturing facility might be applied in Swift utilizing an enum and a switch-case
  • use a protocol in case you are planning to return completely different objects (POP ๐ŸŽ‰)
  • maintain it easy ๐Ÿญ

This sample separates the creation from the precise utilization and strikes the accountability to a particular function, so if one thing modifications you solely have to change the manufacturing facility. You’ll be able to go away all of your checks and every thing else utterly untouched. Highly effective and easy! ๐Ÿ’ช


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.