Last active
October 29, 2018 02:50
-
-
Save j5ik2o/6da17ff112599fd09d716016e81a77e9 to your computer and use it in GitHub Desktop.
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
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 と記述 |
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 domain | |
trait Money { | |
def plus(other: Money): Money | |
def minus(other: Money): Money | |
// ... | |
} |
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 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