Effective Java: security of forwarding classes

Effective java version 3, Item 18: composition over inheritance describes the problem of adding behavior to classes using inheritance:

Recommended solutions:

My question is, does the risk still exist, and methods can also be added to the forwarding class to break the invariants of subclasses? How can an external library like guava adopt an updated method when forwarding classes without risking the integrity of the client?

Solution

The default assumption seems to be that you are the one who writes the forwarding class, so you can control whether anything is added to it In any case, this is a common way to use composition rather than inheritance

Guava examples seem to refer to the forwarding decorators, which are explicitly designed to inherit from But they just make it easier to create these forwarding classes without defining each method in the interface; They explicitly do not protect you from any methods that may need to be covered in the future:

So if I understand all this, guava is not a good example

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>