Skip to content

Instantly share code, notes, and snippets.

@rinotc
Last active August 1, 2021 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rinotc/f1b4f861aa02a0b1825c7f7bded214c7 to your computer and use it in GitHub Desktop.
Save rinotc/f1b4f861aa02a0b1825c7f7bded214c7 to your computer and use it in GitHub Desktop.
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