Saturday, May 18, 2024
HomeIOS DevelopmentSwift static manufacturing facility design sample

Swift static manufacturing facility design sample


Actual world static manufacturing facility sample examples

Named constructors

The primary advantage of the static manufacturing facility sample is that each static manufacturing facility technique can have a reputation. Apple makes use of this sample of their UIColor class implementation to create named colours like .crimson, .yellow, and so on. Please notice that the implementation in Swift will not be actually a technique, however a static property, which returns the precise occasion.

public extension TimeInterval {
    public static var second: TimeInterval { return 1 }
    public static var minute: TimeInterval { return 60 }
    public static var hour: TimeInterval { return 3_600 }
    public static var day: TimeInterval { return 86_400 }
    public static var week: TimeInterval { return 604_800 }
}

If it is so exhausting to memorize what number of seconds is a day or every week why do not we create a named initializer for it. See? TimeInterval.week is a lot better than 604_800. 😅

Cached objects

The subsequent advantage of the static manufacturing facility sample is that it may well assist caching for the sake of higher reminiscence utilization. This manner you’ll be able to restrict the variety of objects created in case you are initializing it by the static constructor (aka. static manufacturing facility technique). 🏭

class Service {

    var title: String

    

    non-public static var cache: [String:Service] = [:]

    non-public static func cached(title: String) -> Service {
        if Service.cache[name] == nil {
            Service.cache[name] = Service(named: title)
        }
        return Service.cache[name]!
    }

    

    static var native: Service {
        return Service.cached(title: "native")
    }

    static var distant: Service {
        return Service.cached(title: "distant")
    }

    

    init(named title: String) {
        self.title = title
    }
}

Native initialization scope

One other advantage of static manufacturing facility strategies you could restrict the initialization of a category to a personal scope degree. In different phrases object creation will solely be accessible by the static manufacturing facility technique. You simply must make the init technique non-public.

public remaining class Service {

    var title: String

    non-public init(title: String) {
        self.title = title
    }

    public static var native: Service {
        return Service(title: "native")
    }

    public static var distant: Service {
        return Service(title: "distant")
    }
}

Observe you could prohibit subclassing with the ultimate & static key phrase. If you wish to enable subclassing you must take away remaining and use the class key phrase as a substitute of the static for the properties, this fashion subclasses can override manufacturing facility strategies. 🤔

Statically return something

Static manufacturing facility also can return subtypes of a given object, however why do not we take this even one step additional? You may also return any form of sort from a static technique, I do know this looks like a cheat, as a result of I am not creating an occasion of UIColor right here, however I consider that it is value to say this technique right here, as a result of it is carefully associated to static factories. This system could be actually helpful generally. 😛

extension UIColor {

    non-public static func picture(with coloration: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, peak: 1)
        UIGraphicsBeginImageContext(rect.measurement)
        let context = UIGraphicsGetCurrentContext()!
        context.setFillColor(coloration.cgColor)
        context.fill(rect)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img!
    }

    static var redImage: UIImage {
        return UIColor.picture(with: .crimson)
    }
}

RELATED ARTICLES

Most Popular

Recent Comments