Last active
May 13, 2020 14:11
-
-
Save abadikaka/cac525c8336d6df5979b1a948cedb0f1 to your computer and use it in GitHub Desktop.
Builder Pattern - Block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We reuse the above SportCarBuilder, we just change some interface | |
// Notice we instead are using the block for update the necessary properties | |
class SportCar { | |
typealias Builder = (builder: SportCarBuilder) -> Void | |
private init(builder: SportCarBuilder) { | |
super.init(doors: Door(2), engine: Engine(V85000), wheel: builder.wheel, color: builder.color) | |
} | |
static func make(with builderBlock: Builder) -> SportCar { | |
let builder = SportCarBuilder() | |
builderBlock(builder) | |
return SportCar(builder: builder) | |
} | |
} | |
// This is the way we will use by defining the properties inside the block | |
let sportCar = SportCar.make { builder in | |
builder.color = .red | |
builder.wheel = .racing | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment