同学去美国了,发来了一道题,这种题在我们学校是不可能有的,声明我们也是211学校,感到有点悲哀。
题目如下:
Part A:
Inheritance is a powerful concept, but is also one of the most abused
concepts in OOP. Give an example where inheritance is a reasonable choice.
Explain why you think it is a good choice in your example. Then give
an example where delegation rather than inheritance is a better choice.
Again explain why you think it is a good choice in your example.
Feel free to use any language you like for your examples.YOUR ANSWER CAN GO HERE OR IN A SEPARATE FILE IN THE HW1 DIRECTORYPart就是比较委托和继承的例子
题目如下:
Part A:
Inheritance is a powerful concept, but is also one of the most abused
concepts in OOP. Give an example where inheritance is a reasonable choice.
Explain why you think it is a good choice in your example. Then give
an example where delegation rather than inheritance is a better choice.
Again explain why you think it is a good choice in your example.
Feel free to use any language you like for your examples.YOUR ANSWER CAN GO HERE OR IN A SEPARATE FILE IN THE HW1 DIRECTORYPart就是比较委托和继承的例子
i am feel so sadder!
很多面向对象的设计专家从1986年就开始警告继承关系被滥用的可能。有一些面向对象的编程语言,如SELF语言,甚至将类的继承从语言的功能中取消掉,改为完全使用委派。
其他的设计师虽然不提倡彻底取消继承,但无一例外地鼓励在设计中尽可能使用委派关系代替继承关系。比如在[GOF95]一书中,状态模式、策略模式、装饰模式、桥梁模式以及抽象工厂模式均是将依赖于继承的实现转换为基于对象的组合和聚合的实现,这些模式的要点就是使用委派关系代替继承关系。
继承,就是把所有东西全先放进来,再把要修改的修改,把要去掉(屏蔽)的去掉。
如果100个东西里,你只是要5个,那么用委托。
如果你是要95个,那么就用继承,当然,也可以用委托,只不过多写95行 ....Combine()
继承,当有优良血统工具包的时候,自身功能又有35%以上的功能重叠
就用继承,但是父类的信息输入和子类要有歧义的一致性。
}委托在信息请求上要有父子集的一致性。所以委托是一种严格模式,没有继承使用方便。所以JDK的工具包也有继承层次过多的情况。个人感觉,未必对!
在一些时候是组合代替了继承,之后又用代理等方式作中间人,破开了组合。