Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

java - Duck example strategy pattern - Head first design pattern

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

  1. Problem enter image description here

  2. the conclusions enter image description here

He said "when joe added new behavior to the duck superclass, he was also adding behavior that was not appropiate for sume Duck subclasses"

BUT in the conclusion he added performFly() and performQuack(); what is the different because i think it same with he was also adding behavior that was not appropiate for sume Duck subclasses?

**image taken from the book Head first design pattern ** this question doesn't state this book is not good, this book is really good in my opinion. this is just me who is asking something that I didn't get from the book.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...