Member-only story

Builder Design Pattern in Swift and iOS/macOS Universe

Nikita Lazarev-Zubov
6 min readJun 22, 2019

--

This time I’d like to talk about another creational design pattern from the Gang of Four’s arsenal — Builder. As it turns out, while having gained my experience I rather often have been caming across this pattern in Java code in general and within Android projects partucularly. But in iOS projects, both Swift and Objective-C, the pattern has been seen quite seldom. Nevertheless, considering its simplicity, the pattern might turn up pretty convenient and, as it’s fashionable to speak, powerful.

This pattern replaces a complex initialization process with step-by-step object construction which is finished by a finalizing method at the end. Herewith, those steps can be optional and must not have a strict sequence of calls.

Foundation Example

Most probably, you’re already familiar with Foundation APIs to construct and URL object. For example, when URL is not fixed and has be constructed from various options of host addresses or resource paths, I bet you used rather convenient class URLComponents.

Mostly, it’s just a class which combines a set of variables that store values of one or another URL component and the url property which returns the corresponding to a current set of components URL. For example:

var urlComponents = URLComponents()…

--

--

Nikita Lazarev-Zubov
Nikita Lazarev-Zubov

Written by Nikita Lazarev-Zubov

Swift and general programming topics, Agile software development, soft skills

No responses yet