Guest User

Untitled

a guest
Aug 26th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.75 KB | None | 0 0
  1. interface Animal {
  2.     val id: String
  3.     val sound: String
  4. }
  5.  
  6. class AnimalBuilder {
  7.     var id: String? = null
  8.     var sound: String? = null
  9.    
  10.     fun withSound(sound: String): AnimalBuilder {
  11.         this.sound = sound
  12.         return this
  13.     }
  14.    
  15.     fun withId(id: String): AnimalBuilder{
  16.         this.id = id
  17.         return this
  18.     }
  19.    
  20.     fun build(): Animal {
  21.         checkNotNull(id)
  22.         checkNotNull(sound)
  23.         val builder = this
  24.         return object: Animal {
  25.             override val id: String = builder.id!!
  26.             override val sound: String = builder.sound!!
  27.         }
  28.     }
  29. }
  30.  
  31. fun main() {
  32.     val dog = AnimalBuilder()
  33.         .withId("3.1415")
  34.         .withSound("Bark")
  35.         .build()
  36. }
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment