Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java like なビルダーパターンのサンプル
class NutrionFacts(builder: NutrionFacts.Builder) {
val servingSize: Int = builder.servingSize
val sergings: Int = builder.sergings
val calories: Int = builder.calories
val fat: Int = builder.fat
val sodium: Int = builder.sodium
val carbohydrate: Int = builder.carbohydrate
}
object NutrionFacts {
class Builder {
var servingSize: Int
var sergings: Int
var calories = 0
var fat = 0
var sodium = 0
var carbohydrate = 0
def setServingSize(servingSize: Int): Builder = {
this.sergingSize = sergingSize
this
}
def setServings(servings: Int): Builder = {
this.sergings = sergings
this
}
def setCalories(calories: Int): Builder = {
this.calories = calories
this
}
def setFat(fat: Int): Builder = {
this.fat = fat
this
}
def setSodium(sodium: Int): Builder = {
this.sodium = sodium
this
}
def setCarbohydrate(carbohydratre: Int) = {
this.carbohydrate = carbohydrate
this
}
def build = new NutrionFacts(this)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment