Created
February 15, 2019 10:52
-
-
Save Shinpeim/200d87d2792c262d5fa7533ca339e7db 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
// 前提として: | |
// FizzBuzzをレイヤードアーキテクチャにする必要はあまり感じないが、 | |
// それでも | |
// 「3の倍数のときは fizz が返る」 | |
// 「5の倍数のときは buzz が返る」 | |
// 「3の倍数かつ5の倍数のときは fizzbuzz が返る」 | |
// 「3の倍数でも5の倍数でもないときはそのままの数字が返る」 | |
// というロジックがdomain layerなのか | |
// usecase layerなのかを無理やり当てはめるのであれば…… | |
// この「ルール」は、1から100まで表示する場合でも | |
// 1から10000まで表示する場合でも変わらない、 | |
// FizzBuzzという問題領域の本質的なルールなので、 | |
// ドメインロジックとすべきだろう。 | |
case class FizzBuzzNum(i: Int) { | |
def asFizzBuzzFormat = i match { | |
case i if i % 15 == 0 => "FizzBuzz" | |
case i if i % 5 == 0 => "Buzz" | |
case i if i % 3 == 0 => "Fizz" | |
case i => i.toString | |
} | |
} | |
// そして、このValueObjectを使って、 | |
// 「1からnまでの数をFizzBuzzルールに従って表示する」という | |
// ユースケースを実現する | |
object FizzBuzzUseCase { | |
def doFizzBuzz(max: Int): Unit = { | |
(1 to max) | |
.map(i => FizzBuzzNum(i).asFizzBuzzFormat) | |
.foreach(println) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment