-
Notifications
You must be signed in to change notification settings - Fork 0
/
Builder.kt
56 lines (44 loc) · 1.41 KB
/
Builder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package builder
/**
* @author ranaaditya
*
* The intent of the Builder design pattern is to separate the construction of a complex object from its representation.
*/
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
}
/**
* Builder class for the outer class,
* adds a level of abstraction in object creation
*/
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)
}
}
// Abstracted and easier call to complex object creation
val foodOrder = FoodOrder.Builder()
.bread("Naan")
.condiments("Chilli sausace")
.fish("sushi")
.meat("freah")
.build()