Created
May 2, 2017 17:21
-
-
Save dhiraj-das/cd40c3cfff72662329ca601a1cd68621 to your computer and use it in GitHub Desktop.
Builder Design Pattern in 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
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