Last active
September 5, 2015 04:11
-
-
Save crowjdh/e0ec0a57329d2ee57b88 to your computer and use it in GitHub Desktop.
Simple builder pattern for Swift
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
import Foundation | |
// Usage: | |
// | |
// let car = Car(engine: "Awesome Engine", gas: 90, builder: { | |
// $0.hasCooler = true | |
// $0.hasSmartKey = true | |
// }) | |
final class Car { | |
let engine: String | |
var gas: Int | |
var hasCooler: Bool? | |
var hasSmartKey = false | |
typealias CarBuilder = (Car) -> () | |
init(engine: String, gas: Int, builder: CarBuilder) { | |
self.engine = engine | |
self.gas = gas | |
self.hasCooler = false | |
builder(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment