Inheritance的优缺点:
可以代码重用。但是子类的行为可能不一致,甚至有些根本就不具有这种行为,这是就需要对每个子类的具体实现进行
覆盖、修改。而且也不能动态对行为进行更换
Interface的优缺点:
灵活。但是代码不能重用
Principle 1:
Idnetify the aspects of your application that vary and separate them from what stays the same.
Principle 2:
Program to an interface, not an implmentation.
这里的interface应该是一个广义的词,supertype的意思
Principle 3:
Favor composition over inheritance.
Composition更具有灵活性,而且可以动态更换
本文对比分析了Inheritance和Interface的优缺点,并提出了三个基本原则,包括识别应用中变化的部分并将其分离、针对接口而非实现编程以及优先使用组合而非继承。
439

被折叠的 条评论
为什么被折叠?



