and I wanted to see if this is the best way to do ...
# getting-started
o
and I wanted to see if this is the best way to do such a thing, or does Kotlin facilitate the purpose of this pattern more easily
a
@oday First of all, there is #codereview for this kind of things
second, inside your
Duck
you have two
lateinit var
. I'd just put them into the constructor to avoid the
lateinit
stuff
abstract class Duck(protected var quackBehavior: Quack, ...)
and third, theoretically the
Strategy Pattern
could be implemented with delegation https://kotlinlang.org/docs/reference/delegation.html, but kotlin doesn't support "replacing" the delegated object at runtime right now https://youtrack.jetbrains.com/issue/KT-5870
o
thanks for that, yea it worked
why do you have them as protected var though? I have them as private val
the constructor params I mean
a
@oday with
protected
the behavior is accessible from subclasses and they can change the behavior at runtime with
quackBehavior = ...
if they aren't gonna change their behavior,
private
is fine too
o
doesn’t seem to hold up
yes it does, sorry!
thanks a lot 🙂
👍 1