Newer
Older
sample01 / builder.kt
yhornisse on 18 Nov 2020 416 bytes add builder pattern
data class Hoge(
  val id:Int,
  val name:String) {
  class HogeBuilder() {
    var id:Int? = null
    var name:String? = null
    fun build() : Hoge = Hoge(
       id = requireNotNull(id),
       name = requireNotNull(name)
     )
   }
}

fun main(args: Array<String>) {
  var builder = Hoge.HogeBuilder()
  builder.id = 1
  builder.name = "hoge"
  var e:Hoge = builder.build()

  println(e.id)
  println(e.name)
}