Created
January 18, 2023 14:46
-
-
Save alibahaaa/c3642f23bc72aae8eb4bd5fbe8db8e77 to your computer and use it in GitHub Desktop.
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 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