[ad_1]
How does the builder sample work?
The builder sample may be applied in a number of methods, however that actually does not issues in case you perceive the principle purpose of the sample:
The intent of the Builder design sample is to separate the development of a fancy object from its illustration.
So in case you have an object with a number of properties, you need to conceal the complexity of the initialization course of, you can write a builder and assemble the article via that. It may be so simple as a construct technique or an exterior class that controls the whole development course of. All of it will depend on the given setting. 🏗
That is sufficient principle 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 imagine that SKEmitterNode is kind of a pleasant instance. If you wish to create customized emitters and set properties programmatically – often for a SpriteKit sport – an emitter builder class like this might be an inexpensive 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 = .crimson
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 to your UIKit software which has many customized fonts, colours, and so on. a builder might be helpful to assemble standalone themes. 🔨
struct Theme {
let textColor: UIColor?
let backgroundColor: UIColor?
}
class ThemeBuilder {
enum Model {
case gentle
case darkish
}
func construct(_ fashion: Model) -> Theme {
swap 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 method you may configure your object via numerous strategies and each single considered one of them will return the identical builder object. This approach you may chain the configuration and as a final step construct the remaining product. ⛓
class URLBuilder {
non-public 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(identify: String, worth: String) -> URLBuilder {
if self.elements.queryItems == nil {
self.elements.queryItems = []
}
self.elements.queryItems?.append(URLQueryItem(identify: identify, 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(identify: "type", worth: "identify")
.addQueryItem(identify: "order", worth: "asc")
.construct()
The builder sample with a director
Let’s meet the director object. Because it looks as if this little factor decouples the builder from the precise configuration half. So as an illustration you can also make a sport with circles, however in a while in case you change your thoughts and you need to make use of squares, that is comparatively simple. You simply need to create a brand new builder, and every part else may be the identical. 🎬
protocol NodeBuilder {
var identify: String { get set }
var colour: SKColor { get set }
var dimension: CGFloat { get set }
func construct() -> SKShapeNode
}
protocol NodeDirector {
var builder: NodeBuilder { get set }
func construct() -> SKShapeNode
}
class CircleNodeBuilder: NodeBuilder {
var identify: String = ""
var colour: SKColor = .clear
var dimension: CGFloat = 0
func construct() -> SKShapeNode {
let node = SKShapeNode(circleOfRadius: self.dimension)
node.identify = self.identify
node.fillColor = self.colour
return node
}
}
class PlayerNodeDirector: NodeDirector {
var builder: NodeBuilder
init(builder: NodeBuilder) {
self.builder = builder
}
func construct() -> SKShapeNode {
self.builder.identify = "Whats up"
self.builder.dimension = 32
self.builder.colour = .crimson
return self.builder.construct()
}
}
let builder = CircleNodeBuilder()
let director = PlayerNodeDirector(builder: builder)
let participant = director.construct()
Block primarily based builders
A extra swifty method may be the usage of blocks as an alternative of builder lessons to configure objects. After all 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 = "Whats up wold!"
label.font = UIFont.systemFont(ofSize: 12)
}
Please observe that the builder implementation can fluctuate on the precise use case. Typically a builder is mixed with factories. So far as I can see nearly everybody interpreted it another way, however I do not assume that is an issue. Design patterns are well-made pointers, however typically you need to cross the road.
[ad_2]