BAEL-1840 Builder Pattern in Kotlin (#4730)
* builder pattern in kotlin * builder pattern in kotlin new-line * deleted Sandbox, added unit test * add other tests * named and default parameters builder * Make FoodOrderNamed a data class
This commit is contained in:
parent
3b0fef0266
commit
4aa77eb55d
|
@ -0,0 +1,39 @@
|
|||
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
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.builder
|
||||
|
||||
class FoodOrderApply {
|
||||
var bread: String? = null
|
||||
var condiments: String? = null
|
||||
var meat: String? = null
|
||||
var fish: String? = null
|
||||
}
|
|
@ -0,0 +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)
|
|
@ -0,0 +1,139 @@
|
|||
package com.baeldung.builder
|
||||
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class BuilderPatternUnitTest {
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderSettingValues_thenFieldsNotNull() {
|
||||
|
||||
val foodOrder = FoodOrder.Builder()
|
||||
.bread("white bread")
|
||||
.meat("bacon")
|
||||
.fish("salmon")
|
||||
.condiments("olive oil")
|
||||
.build()
|
||||
|
||||
Assertions.assertNotNull(foodOrder.bread)
|
||||
Assertions.assertNotNull(foodOrder.meat)
|
||||
Assertions.assertNotNull(foodOrder.condiments)
|
||||
Assertions.assertNotNull(foodOrder.fish)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderSettingValues_thenFieldsContainsValues() {
|
||||
|
||||
val foodOrder = FoodOrder.Builder()
|
||||
.bread("white bread")
|
||||
.meat("bacon")
|
||||
.fish("salmon")
|
||||
.condiments("olive oil")
|
||||
.build()
|
||||
|
||||
Assertions.assertEquals("white bread", foodOrder.bread)
|
||||
Assertions.assertEquals("bacon", foodOrder.meat)
|
||||
Assertions.assertEquals("olive oil", foodOrder.condiments)
|
||||
Assertions.assertEquals("salmon", foodOrder.fish)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderWithoutSettingValues_thenFieldsNull() {
|
||||
|
||||
val foodOrder = FoodOrder.Builder()
|
||||
.build()
|
||||
|
||||
Assertions.assertNull(foodOrder.bread)
|
||||
Assertions.assertNull(foodOrder.meat)
|
||||
Assertions.assertNull(foodOrder.condiments)
|
||||
Assertions.assertNull(foodOrder.fish)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderNamedSettingValues_thenFieldsNotNull() {
|
||||
|
||||
val foodOrder = FoodOrderNamed(
|
||||
meat = "bacon",
|
||||
fish = "salmon",
|
||||
condiments = "olive oil",
|
||||
bread = "white bread"
|
||||
)
|
||||
|
||||
Assertions.assertNotNull(foodOrder.bread)
|
||||
Assertions.assertNotNull(foodOrder.meat)
|
||||
Assertions.assertNotNull(foodOrder.condiments)
|
||||
Assertions.assertNotNull(foodOrder.fish)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderNamedSettingValues_thenFieldsContainsValues() {
|
||||
|
||||
val foodOrder = FoodOrderNamed(
|
||||
meat = "bacon",
|
||||
fish = "salmon",
|
||||
condiments = "olive oil",
|
||||
bread = "white bread"
|
||||
)
|
||||
|
||||
Assertions.assertEquals("white bread", foodOrder.bread)
|
||||
Assertions.assertEquals("bacon", foodOrder.meat)
|
||||
Assertions.assertEquals("olive oil", foodOrder.condiments)
|
||||
Assertions.assertEquals("salmon", foodOrder.fish)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderNamedWithoutSettingValues_thenFieldsNull() {
|
||||
|
||||
val foodOrder = FoodOrderNamed()
|
||||
|
||||
Assertions.assertNull(foodOrder.bread)
|
||||
Assertions.assertNull(foodOrder.meat)
|
||||
Assertions.assertNull(foodOrder.condiments)
|
||||
Assertions.assertNull(foodOrder.fish)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderApplySettingValues_thenFieldsNotNull() {
|
||||
|
||||
val foodOrder = FoodOrderApply().apply {
|
||||
meat = "bacon"
|
||||
fish = "salmon"
|
||||
condiments = "olive oil"
|
||||
bread = "white bread"
|
||||
}
|
||||
|
||||
Assertions.assertNotNull(foodOrder.bread)
|
||||
Assertions.assertNotNull(foodOrder.meat)
|
||||
Assertions.assertNotNull(foodOrder.condiments)
|
||||
Assertions.assertNotNull(foodOrder.fish)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderApplySettingValues_thenFieldsContainsValues() {
|
||||
|
||||
val foodOrder = FoodOrderApply().apply {
|
||||
meat = "bacon"
|
||||
fish = "salmon"
|
||||
condiments = "olive oil"
|
||||
bread = "white bread"
|
||||
}
|
||||
|
||||
Assertions.assertEquals("white bread", foodOrder.bread)
|
||||
Assertions.assertEquals("bacon", foodOrder.meat)
|
||||
Assertions.assertEquals("olive oil", foodOrder.condiments)
|
||||
Assertions.assertEquals("salmon", foodOrder.fish)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenBuildingFoodOrderApplyWithoutSettingValues_thenFieldsNull() {
|
||||
|
||||
val foodOrder = FoodOrderApply()
|
||||
|
||||
Assertions.assertNull(foodOrder.bread)
|
||||
Assertions.assertNull(foodOrder.meat)
|
||||
Assertions.assertNull(foodOrder.condiments)
|
||||
Assertions.assertNull(foodOrder.fish)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue