From cb86a05efd5a057677a3d7669f7853c653d58fe1 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 18 Aug 2018 14:01:01 +0200 Subject: [PATCH] Kotlin Builder example refactor (#4895) * encoding * Refactor builder * Usage * Builder as data class --- .../kotlin/com/baeldung/builder/FoodOrder.kt | 43 ++++++------------- .../com/baeldung/builder/FoodOrderNamed.kt | 8 ++-- .../main/kotlin/com/baeldung/builder/Main.kt | 9 ++++ 3 files changed, 26 insertions(+), 34 deletions(-) create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/builder/Main.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/builder/FoodOrder.kt b/core-kotlin/src/main/kotlin/com/baeldung/builder/FoodOrder.kt index 7d10d849b9..1384cd9937 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/builder/FoodOrder.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/builder/FoodOrder.kt @@ -1,39 +1,22 @@ package com.baeldung.builder -class FoodOrder private constructor(builder: FoodOrder.Builder) { - - val bread: String? - val condiments: String? - val meat: String? - val fish: String? - - init { - this.bread = builder.bread - this.condiments = builder.condiments - this.meat = builder.meat - this.fish = builder.fish - } - - class Builder { - - var bread: String? = null - private set - var condiments: String? = null - private set - var meat: String? = null - private set - var fish: String? = null - private set +class FoodOrder( + val bread: String?, + val condiments: String?, + val meat: String?, + val fish: String? +) { + data class Builder( + var bread: String? = null, + var condiments: String? = null, + var meat: String? = null, + var fish: String? = null) { fun bread(bread: String) = apply { this.bread = bread } - fun condiments(condiments: String) = apply { this.condiments = condiments } - fun meat(meat: String) = apply { this.meat = meat } - fun fish(fish: String) = apply { this.fish = fish } - - fun build() = FoodOrder(this) - + fun build() = FoodOrder(bread, condiments, meat, fish) } } + diff --git a/core-kotlin/src/main/kotlin/com/baeldung/builder/FoodOrderNamed.kt b/core-kotlin/src/main/kotlin/com/baeldung/builder/FoodOrderNamed.kt index 6e20cf51b9..0e4219b40e 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/builder/FoodOrderNamed.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/builder/FoodOrderNamed.kt @@ -1,7 +1,7 @@ package com.baeldung.builder data class FoodOrderNamed( - val bread: String? = null, - val condiments: String? = null, - val meat: String? = null, - val fish: String? = null) + val bread: String? = null, + val condiments: String? = null, + val meat: String? = null, + val fish: String? = null) diff --git a/core-kotlin/src/main/kotlin/com/baeldung/builder/Main.kt b/core-kotlin/src/main/kotlin/com/baeldung/builder/Main.kt new file mode 100644 index 0000000000..cc348e3fbf --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/builder/Main.kt @@ -0,0 +1,9 @@ +package com.baeldung.builder + +fun main(args: Array) { + FoodOrder.Builder() + .bread("bread") + .condiments("condiments") + .meat("meat") + .fish("bread").let { println(it) } +} \ No newline at end of file