Last active
August 1, 2021 03:12
-
-
Save rinotc/f1b4f861aa02a0b1825c7f7bded214c7 to your computer and use it in GitHub Desktop.
Java like なビルダーパターンのサンプル
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
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