Duck example strategy pattern - Head first design pattern

前端 未结 4 1469
鱼传尺愫
鱼传尺愫 2020-12-16 22:01

I want to ask something about duck example on this book that made me confused and I feel contradictions.

  1. Problem

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 22:44

    I'm not a guru of design patterns, but while I was reading that book, the first sensation I had about that particular chapter was that the way interfaces was built and then implemented, violated one of the well know programming principle: the Interface Segregation Principle (ISP) Basically this principle state that

    no client should be forced to depend on methods it does not use

    Because some Ducks that don't fly implement the fly() method even it they don't need it. That said, I think that in this particular case it is unavoidable to implement all the interfaces methods since on the client side we're using polymorphic behaviors, and we need to be sure that we have all the methods available even if unused.

提交回复
热议问题