I came across that when I was trying to implement the Strategy Pattern in Head First Design Pattern.
Therefore, the following code does not work.
val rubberDuck: RubberDuck = RubberDuck(FlyNoWay(), Squeak())
rubberDuck.display()
rubberDuck.fly()
rubberDuck.quack()
println("=================================================")
println()
makeRubberFlyable(rubberDuck)
}
fun makeRubberFlyable(rubberDuck: RubberDuck) {
println("Make Rubber Duck Flyable!")
rubberDuck.flyBehavior = FlyWithRocket()
rubberDuck.fly()
}
Is there any reason to always forward to the initial value instead of forwarding to the content of var
?
Thanks.