Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Animal {
- val id: String
- val sound: String
- }
- class AnimalBuilder {
- var id: String? = null
- var sound: String? = null
- fun withSound(sound: String): AnimalBuilder {
- this.sound = sound
- return this
- }
- fun withId(id: String): AnimalBuilder{
- this.id = id
- return this
- }
- fun build(): Animal {
- checkNotNull(id)
- checkNotNull(sound)
- val builder = this
- return object: Animal {
- override val id: String = builder.id!!
- override val sound: String = builder.sound!!
- }
- }
- }
- fun main() {
- val dog = AnimalBuilder()
- .withId("3.1415")
- .withSound("Bark")
- .build()
- }
Advertisement
Add Comment
Please, Sign In to add comment