Created
August 14, 2018 17:09
-
-
Save linux-china/ba9793ce45c28ff9619e19dc7c8cdfab to your computer and use it in GitHub Desktop.
Kotlin Builder Pattern Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mvnsearch.builder | |
class FoodOrder private constructor(builder: FoodOrder.Builder) { | |
val bread: String? | |
val meat: String? | |
init { | |
this.bread = builder.bread | |
this.meat = builder.meat | |
} | |
class Builder { | |
var bread: String? = null | |
private set | |
var meat: String? = null | |
private set | |
fun bread(bread: String) = apply { this.bread = bread } | |
fun meat(meat: String) = apply { this.meat = meat } | |
fun build() = FoodOrder(this) | |
} | |
} | |
fun main(args: Array<String>) { | |
val foodOrder = FoodOrder.Builder().bread("Italian").meat("Steak").build(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment