Skip to content

Instantly share code, notes, and snippets.

@dhiraj-das
Created May 2, 2017 17:21
Show Gist options
  • Save dhiraj-das/cd40c3cfff72662329ca601a1cd68621 to your computer and use it in GitHub Desktop.
Save dhiraj-das/cd40c3cfff72662329ca601a1cd68621 to your computer and use it in GitHub Desktop.
Builder Design Pattern in Swift
class HouseBuilder {
var color: UIColor = UIColor.white
var noOfRooms: Int
var noOfBathrooms: Int = 1
var hasBalcony: Bool = false
init(noOfRooms: Int) {
self.noOfRooms = noOfRooms
}
func set(color: UIColor) -> HouseBuilder {
self.color = color
return self
}
func set(noOfBathrooms: Int) -> HouseBuilder {
self.noOfBathrooms = noOfBathrooms
return self
}
func addBalcony() -> HouseBuilder {
hasBalcony = true
return self }
func build() -> House {
return House(builder: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment