Swift easy manufacturing facility design sample



ยท 1 min learn


This time let’s speak in regards to the easy manufacturing facility design sample to encapsulate object creation in a extremely easy approach utilizing Swift.

Easy manufacturing facility implementation utilizing switch-case

The purpose of this sample is to encapsulate one thing that may typically range. Think about a colour palette for an utility. You may need to vary the colours based on the newest behavior of the designer every day. Iโ€™d be actually inconvenient in the event you needed to search & exchange 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 {
        swap 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 an advanced object initialization course of. You can too outline a protocol and return varied occasion varieties that implement the required interface utilizing a swap 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 {
        swap kind {
        case .dev:
            return DevEnvironment()
        case .stay:
            return LiveEnvironment()
        }
    }
}

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

So, just a few 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 could be applied in Swift utilizing an enum and a switch-case
+ use a protocol in case you are planning to return totally different objects (POP ๐ŸŽ‰)
+ preserve it easy ๐Ÿญ

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

Associated posts


ยท 5 min learn


On this article I’m going to point out you how you can implement a fundamental occasion processing system on your modular Swift utility.


ยท 4 min learn


Be taught the iterator design sample by utilizing some customized sequences, conforming to the IteratorProtocol from the Swift normal library.


ยท 4 min learn


Discover ways to use lazy properties in Swift to enhance efficiency, keep away from optionals or simply to make the init course of extra clear.


ยท 5 min learn


Newbie’s information about optics in Swift. Discover ways to use lenses and prisms to govern objects utilizing a practical strategy.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles