composite VS. inheritance

原帖link:http://www.javaworld.com/jw-11-1998/jw-11-techniques.html?page=3

Inheritance:

When you establish an inheritance relationship between two classes, you get to take advantage of dynamic binding andpolymorphism. Dynamic binding means the JVM will decide at runtime which method implementation to invoke based on the class of the object. Polymorphism means you can use a variable of a superclass type to hold a reference to an object whose class is the superclass or any of its subclasses.

One of the prime benefits of dynamic binding and polymorphism is that they can help make code easier to change. If you have a fragment of code that uses a variable of a superclass type, such as Fruit, you could later create a brand new subclass, such as Banana, and the old code fragment will work without change with instances of the new subclass. If Bananaoverrides any of Fruit's methods that are invoked by the code fragment, dynamic binding will ensure that Banana's implementation of those methods gets executed. This will be true even though class Banana didn't exist when the code fragment was written and compiled.

In an inheritance relationship, superclasses are often said to be "fragile," because one little change to a superclass can ripple out and require changes in many other places in the application's code. 

For example, if you change the return type of a public method in class Fruit (a part of Fruit's interface), you can break the code that invokes that method on any reference of type Fruit or any subclass of Fruit. In addition, you break the code that defines any subclass of Fruit that overrides the method. Such subclasses won't compile until you go and change the return value of the overridden method to match the changed method in superclassFruit.

Inheritance is also sometimes said to provide "weak encapsulation," because if you have code that directly uses a subclass, such asApple, that code can be broken by changes to a superclass, such as Fruit. One of the ways to look at inheritance is that it allows subclass code to reuse superclass code. For example, if Appledoesn't override a method defined in its superclass FruitApple is in a sense reusing Fruit's implementation of the method. But Appleonly "weakly encapsulates" the Fruit code it is reusing, because changes to Fruit's interface can break code that directly usesApple.

总结:对于inheritance,如果你改变了base class的code,不仅derived class可能编译不过或者不能运行,连使用了derived class的code部分也不能reuse了。所以是"weak encapsulation"

Composite:

Composition provides an alternative way for Apple to reuse Fruit's implementation ofpeel(). Instead of extending FruitApple can hold a reference to a Fruit instance and define its own peel() method that simply invokes peel() on the Fruit.

In the composition approach, the subclass becomes the "front-end class," and the superclass becomes the "back-end class." With inheritance, a subclass automatically inherits an implemenation of any non-private superclass method that it doesn't override. With composition, by contrast, the front-end class must explicitly invoke a corresponding method in the back-end class from its own implementation of the method. This explicit call is sometimes called "forwarding" or "delegating" the method invocation to the back-end object. The composition approach to code reuse provides stronger encapsulation than inheritance, because a change to a back-end class needn't break any code that relies only on the front-end class. 

总结:

1. It is easier to change the interface of the back-end class then base class. 
    Composite: the change of the interface of the back-end class needs the change of the front-end class implementation, But not necessarily the change of the front-end interface.
    Inheritance: the change of base class interface  not only ripple down the inheritance hierarchy to derived class, but can also ripple out to code that uses just the derived class interface.

2. Composition allows you to delay the creation of back-end objects until (and unless) they are needed, as well as changing the back-end objects dynamically throughout the lifetime of the front-end object. 

3. It is easier to add new subclasses (inheritance) than it is to add new front-end classes (composition), because inheritance comes with polymorphism.

什么时候使用:

1. Make sure inheritance models the is-a relationship, for example, banana is a fruit. An important question to ask yourself when you think you have an is-a relationship is whether that is-a relationship will be constant throughout the lifetime of the application and, with luck, the lifecycle of the code. For example, you might think that an Employee is-aPerson, when really Employee represents a role that a Person plays part of the time. What if the person becomes unemployed? What if the person is both an Employee and a Supervisor? Such impermanent is-a relationships should usually be modelled with composition.

2. Don't use inheritance just to get code reuse. If all you really want is to reuse code and there is no is-a relationship in sight, use composition.
Don't use inheritance just to get at polymorphism. If all you really want is polymorphism, but there is no natural is-a relationship, use composition with interfaces. I'll be talking about this subject next month.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值