Tuesday, June 30, 2026
HomeiOS DevelopmentSwift static manufacturing unit design sample

Swift static manufacturing unit design sample

[ad_1]

Named constructors

The primary benefit of the static facotry sample is that each static facotry methodology can have a reputation. Apple makes use of this sample of their UIColor class implementation to create named colours like .crimson, .yellow, and so forth. Please observe that the implementation in Swift just isn’t actually a way, 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 per week why do not we create a named initializer for it. See? TimeInterval.week is significantly better than 604_800. 😅

Native initialization scope

One other benefit of static manufacturing unit strategies that you may restrict the initialization of a category to a personal scope degree. In different phrases object creation will solely be out there via the static manufacturing unit methodology. You simply need to make the init methodology non-public.

public closing 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")
    }
}

Be aware that you may prohibit subclassing with the ultimate & static key phrase. If you wish to enable subclassing it is best to take away closing and use the class key phrase as a substitute of the static for the properties, this manner subclasses can override manufacturing unit strategies. 🤔

Statically return something

Static manufacturing unit also can return subtypes of a given object, however why do not we take this even one step additional? You can too return any form of sort from a static methodology, I do know this looks as if a cheat, as a result of I am not creating an occasion of UIColor right here, however I imagine that it is price to say this methodology right here, becuase it is carefully associated to static factories. This method will be actually helpful typically. 😛

extension UIColor {

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

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

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments