Skip to content

Instantly share code, notes, and snippets.

@alibahaaa
Created January 18, 2023 14:46
Embed
What would you like to do?
class User {
var name: String = ""
var email: String = ""
var age: Int = 0
var address: String = ""
override fun toString(): String {
return "User(name='$name', email='$email', age=$age, address='$address')"
}
}
class UserBuilder {
private var user = User()
fun name(name: String): UserBuilder {
user.name = name
return this
}
fun email(email: String): UserBuilder {
user.email = email
return this
}
fun age(age: Int): UserBuilder {
user.age = age
return this
}
fun address(address: String): UserBuilder {
user.address = address
return this
}
fun build(): User {
return user
}
}
val user = UserBuilder()
.name("John Smith")
.email("john.smith@example.com")
.age(30)
.address("123 Main St")
.build()
println(user)
// Output: User(name='John Smith', email='john.smith@example.com', age=30, address='123 Main St')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment