有鑒於本人的金魚腦,讀到現在已經快要忘記書中提過的設計原則,所以來幫自己恢復一下記憶~
Encapsulate what varies:
Duck()
可以飛或叫,但每種鴨子叫聲都不同,所以把需要變化的部分封裝成QuackBehavior()
和FlyBehavior()
介面,再由實體介面去實作例如 FlyWithwings()
, FlyNoWay()
Favor composition over inheritance:
Program to interface, not implementations
Strive for loosely coupled designs between objects that interact.
Classes should be open for extension but closed for modification
Depend on abstractions. Do not depend on concrete classes.
下一篇就會進到下個章節 The Singleton Pattern →
https://github.com/changtintin/Design-Pattern/tree/master/Ch2/Java
Disclaimer
因為讀的是原文版,所以難免會有翻譯詞不達意或是專有名詞上的差異,有錯誤的話歡迎在留言區一起交流!