It's also important to understand the difference between class inheritance and interface inheritance (or
subtyping). Class inheritance defines an object's implementation in terms of another object's implementation.
In short, it's a mechanism for code and representation sharing. In contrast, interface inheritance (or
subtyping) describes when an object can be used in place of another.
Many of the design patterns depend on this distinction. For example, objects in a Chain of Responsibility
(223) must have a common type, but usually they don't share a common implementation. In the Composite
(163) pattern, Component defines a common interface, but Composite often defines a common
implementation. Command (233), Observer (293), State (305), and Strategy (315) are often implemented
with abstract classes that are pure interfaces.
本文探讨了类继承与接口继承的区别:类继承关注实现复用,而接口继承关注对象间替换的可能性。许多设计模式如职责链、组合模式等依赖于这一区别。
1160

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



