How does the builder sample work?
The builder sample could be carried out in a number of methods, however that basically would not issues if you happen to perceive the primary purpose of the sample:
The intent of the Builder design sample is to separate the development of a posh object from its illustration.
So when you have an object with numerous properties, you wish to disguise the complexity of the initialization course of, you may write a builder and assemble the thing via that. It may be so simple as a construct technique or an exterior class that controls your entire building course of. All of it will depend on the given surroundings. 🏗
That is sufficient concept for now, let’s examine the builder sample in motion utilizing dummy, however real-world examples and the highly effective Swift programming language! 💪
Easy emitter builder
I consider that SKEmitterNode is kind of a pleasant instance. If you wish to create customized emitters and set properties programmatically – normally for a SpriteKit recreation – an emitter builder class like this may very well be an affordable resolution. 👾
class EmitterBuilder {
func construct() -> SKEmitterNode {
let emitter = SKEmitterNode()
emitter.particleTexture = SKTexture(imageNamed: "MyTexture")
emitter.particleBirthRate = 100
emitter.particleLifetime = 60
emitter.particlePositionRange = CGVector(dx: 100, dy: 100)
emitter.particleSpeed = 10
emitter.particleColor = .pink
emitter.particleColorBlendFactor = 1
return emitter
}
}
EmitterBuilder().construct()
Easy theme builder
Let’s transfer away from gaming and picture that you’re making a theme engine in your UIKit utility which has many customized fonts, colours, and many others. a builder may very well be helpful to assemble standalone themes. 🔨
struct Theme {
let textColor: UIColor?
let backgroundColor: UIColor?
}
class ThemeBuilder {
enum Type {
case gentle
case darkish
}
func construct(_ fashion: Type) -> Theme {
change fashion {
case .gentle:
return Theme(textColor: .black, backgroundColor: .white)
case .darkish:
return Theme(textColor: .white, backgroundColor: .black)
}
}
}
let builder = ThemeBuilder()
let gentle = builder.construct(.gentle)
let darkish = builder.construct(.darkish)
"Chained" URL builder
With this strategy you'll be able to configure your object via numerous strategies and each single considered one of them will return the identical builder object. This means you'll be able to chain the configuration and as a final step construct the last product. ⛓
class URLBuilder {
personal var elements: URLComponents
init() {
self.elements = URLComponents()
}
func set(scheme: String) -> URLBuilder {
self.elements.scheme = scheme
return self
}
func set(host: String) -> URLBuilder {
self.elements.host = host
return self
}
func set(port: Int) -> URLBuilder {
self.elements.port = port
return self
}
func set(path: String) -> URLBuilder {
var path = path
if !path.hasPrefix("/") {
path = "/" + path
}
self.elements.path = path
return self
}
func addQueryItem(title: String, worth: String) -> URLBuilder {
if self.elements.queryItems == nil {
self.elements.queryItems = []
}
self.elements.queryItems?.append(URLQueryItem(title: title, worth: worth))
return self
}
func construct() -> URL? {
return self.elements.url
}
}
let url = URLBuilder()
.set(scheme: "https")
.set(host: "localhost")
.set(path: "api/v1")
.addQueryItem(title: "kind", worth: "title")
.addQueryItem(title: "order", worth: "asc")
.construct()
The builder sample with a director
Let’s meet the director object. Because it looks like this little factor decouples the builder from the precise configuration half. So as an example you may make a recreation with circles, however afterward if you happen to change your thoughts and you would like to make use of squares, that is comparatively straightforward. You simply should create a brand new builder, and all the things else could be the identical. 🎬
protocol NodeBuilder {
var title: String { get set }
var colour: SKColor { get set }
var measurement: CGFloat { get set }
func construct() -> SKShapeNode
}
protocol NodeDirector {
var builder: NodeBuilder { get set }
func construct() -> SKShapeNode
}
class CircleNodeBuilder: NodeBuilder {
var title: String = ""
var colour: SKColor = .clear
var measurement: CGFloat = 0
func construct() -> SKShapeNode {
let node = SKShapeNode(circleOfRadius: self.measurement)
node.title = self.title
node.fillColor = self.colour
return node
}
}
class PlayerNodeDirector: NodeDirector {
var builder: NodeBuilder
init(builder: NodeBuilder) {
self.builder = builder
}
func construct() -> SKShapeNode {
self.builder.title = "Howdy"
self.builder.measurement = 32
self.builder.colour = .pink
return self.builder.construct()
}
}
let builder = CircleNodeBuilder()
let director = PlayerNodeDirector(builder: builder)
let participant = director.construct()
Block based mostly builders
A extra swifty strategy could be the usage of blocks as an alternative of builder lessons to configure objects. In fact we may argue on if that is nonetheless a builder sample or not… 😛
extension UILabel {
static func construct(block: ((UILabel) -> Void)) -> UILabel {
let label = UILabel(body: .zero)
block(label)
return label
}
}
let label = UILabel.construct { label in
label.translatesAutoresizingMaskIntoConstraints = false
label.textual content = "Howdy wold!"
label.font = UIFont.systemFont(ofSize: 12)
}
Please be aware that the builder implementation can fluctuate on the precise use case. Typically a builder is mixed with factories. So far as I can see virtually everybody interpreted it differently, however I do not assume that is an issue. Design patterns are well-made pointers, however typically it’s important to cross the road.