Skip to content

Instantly share code, notes, and snippets.

@abadikaka
Last active May 13, 2020 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abadikaka/cac525c8336d6df5979b1a948cedb0f1 to your computer and use it in GitHub Desktop.
Save abadikaka/cac525c8336d6df5979b1a948cedb0f1 to your computer and use it in GitHub Desktop.
Builder Pattern - Block
// 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