Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Last active October 29, 2018 02:50
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 j5ik2o/6da17ff112599fd09d716016e81a77e9 to your computer and use it in GitHub Desktop.
Save j5ik2o/6da17ff112599fd09d716016e81a77e9 to your computer and use it in GitHub Desktop.
val m1: Money = ...
val m2: Money = ...
// 貧血症ドメインモデルはコードの意図が読み取りづらい
m1.checkHasSameCurrencyAs(m2)
val m3: Money = Money.adjustBy(m1.amount + m2.amount, m1.currency)
// 表現力の高いドメインモデルは設計の意図を明白にする
val m4: Money = m1.plus(m2) // もしくは m1 + m2 と記述
package domain
trait Money {
def plus(other: Money): Money
def minus(other: Money): Money
// ...
}
package domain
import java.util.Currency
final class MoneyImpl(amount: BigDecimal,
currency: Currency) extends Money {
require(amount.scale == currency.getDefaultFractionDigits,
"Scale of amount does not match currency")
override def plus(other: Money): Money = {
checkHasSameCurrencyAs(other)
Money.adjustBy(amount + other.amount, currency)
}
override def minus(other: Money): Money = plus(other.negated)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment