June 2018
Intermediate to advanced
310 pages
6h 32m
English
The previous approach has most of the logic in our context. You may sometimes see a different approach, which is valid as your context grows bigger.
In this approach, Snail would become really thin:
class Snail { internal var mood: Mood = Still(this) private var healthPoints = 10 // That's all!}
Note that we marked mood as internal. That lets other classes in that package alter it.
Instead of Snail implementing WhatCanHappen, our Mood will:
sealed class Mood : WhatCanHappen
And now the logic resides within our state objects:
class Still(private val snail: Snail) : Mood() { override fun seeHero() = snail.mood.run { Aggressive(snail) } override fun getHit(pointsOfDamage: Int) = this override fun timePassed() = this}
Note ...
Read now
Unlock full access