The two most common techniques for reusing functionality in object-oriented systems are class inheritance
and object composition. As we've explained, class inheritance lets you define the implementation of one
class in terms of another's. Reuse by subclassing is often referred to as white-box reuse. The term "whitebox"
refers to visibility: With inheritance, the internals of parent classes are often visible to subclasses.
Object composition is an alternative to class inheritance. Here, new functionality is obtained by assembling
or composing objects to get more complex functionality. Object composition requires that the objects being
composed have well-defined interfaces. This style of reuse is called black-box reuse, because no internal
details of objects are visible. Objects appear only as "black boxes."
Inheritance and composition each have their advantages and disadvantages. Class inheritance is defined
statically at compile-time and is straightforward to use, since it's supported directly by the programming
language. Class inheritance also makes it easier to modify the implementation being reused. When a
subclass overrides some but not all operations, it can affect the operations it inherits as well, assuming they
call the overridden operations.
But class inheritance has some disadvantages, too. First, you can't change the implementations inherited
from parent classes at run-time, because inheritance is defined at compile-time. Second, and generally
worse, parent classes often define at least part of their subclasses' physical representation. Because
inheritance exposes a subclass to details of its parent's implementation, it's often said that "inheritance
breaks encapsulation" [Sny86]. The implementation of a subclass becomes so bound up with the
implementation of its parent class that any change in the parent's implementation will force the subclass to
change.
Implementation dependencies can cause problems when you're trying to reuse a subclass. Should any aspect
of the inherited implementation not be appropriate for new problem domains, the parent class must be
rewritten or replaced by something more appropriate. This dependency limits flexibility and ultimately
reusability. One cure for this is to inherit only from abstract classes, since they usually provide little or no
implementation.
Object composition is defined dynamically at run-time through objects acquiring references to other objects.
Composition requires objects to respect each others' interfaces, which in turn requires carefully designed
interfaces that don't stop you from using one object with many others. But there is a payoff. Because objects
are accessed solely through their interfaces, we don't break encapsulation. Any object can be replaced at runtime
by another as long as it has the same type. Moreover, because an object's implementation will be
written in terms of object interfaces, there are substantially fewer implementation dependencies.
Object composition has another effect on system design. Favoring object composition over class inheritance
helps you keep each class encapsulated and focused on one task. Your classes and class hierarchies will
remain small and will be less likely to grow into unmanageable monsters. On the other hand, a design based
on object composition will have more objects (if fewer classes), and the system's behavior will depend on
their interrelationships instead of being defined in one class.
分享到:
相关推荐
AdapterDelegates, 用于RecyclerView适配器的"Favor composition over inheritance" AdapterDelegates阅读这个项目的动机在我的博客文章。依赖项这里库在 Maven 中心可用:compile '...
这倒也符合 GoF 的《Design Patterns-Elements of Reusable of Object-Oriented Software》书中的一个建议:Favor object composition over class inheritance,即优先使用对象组合而不是继承。 PML 中的对象...
在面向对象编程中,有两种重要的设计原则:Program to an interface, not an implementation和Favor object composition over class inheritance。前者强调使用者不需要知道数据类型、结构、算法的细节,只需要知道...
> Favor composition over inheritance. LovelyDialog doesn't subclass any Dialog related classes, it is just a lightweight extensible wrapper for Dialog and manipulations with custom view. If you would ...
【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the(72页),资源名称:【英文】德银报告:中国电子商务报告China E-commerce:A tale of two camps - and we favor the...
在IT行业中,尤其是在开发用户界面时,数据验证是至关重要的一个环节。Flex作为一个开源的、基于ActionScript的框架,主要用于创建富互联网应用程序(RIA)。它提供了丰富的组件库,其中包括用于数据验证的类,使得...
- 继承和组合的使用场景选择,尽量减少浅继承,提倡"favor object composition over class inheritance"。 - 使用const和引用参数,提高程序安全性。 - 设计模式:单例、工厂、观察者等设计模式在面试中也常被...
目的:将所有显示的图形从脚本保存到任何目录路径。 指示: - 如果在当前工作目录中,将函数 save_all_figures_to_directory.m 放在第一位- 第二次从脚本中调用函数 save_all_figures_to_directory(dir_name, extra)...
《Polar Favor用户使用说明书》提供了详尽的指南,帮助用户了解并充分利用Polar Favor心率监测设备。以下是对说明书内容的详细解析: 1. **Polar Favor的工作原理**(第5页):Polar Favor是一款智能心率监测器,...
Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control Declaration Order Write Short Functions Google-Specific Magic Smart Pointers cpplint Other C++ Features ...
C#面向对象的基本原则 一、面向接口编成而不是实现 [Code to an interface rather than to an implementation.] 二、优先使用组合而非继承 [Favor Composition Over Inheritance.] 三、SRP: The single ...
Based on https://github.com/sockeqwe/AdapterDelegates." Favor composition over inheritance" for ListView Features Composable view handling on ListView Plugable data handler ViewHolder pattern for ...
Favor composition over inheritance 想法是为每种视图类型定义一个AdapterDelegate。 该委托负责创建ViewHolder并为特定视图类型绑定ViewHolder。 然后,您可以通过注册您真正需要的AdapterDelegates来组成...
- "Personally, I am in favor of the former point of view." 这句话表明了对某一观点的赞同,"former"可以替换为其他形容词来指代不同的观点。 - "To my point of view,..." 或 "To my mind,..." 是另一种表达...
优先使用组合而非继承 (Favor Composition Over Inheritance) 在设计类结构时,应优先考虑使用组合(has-a)关系,而不是继承(is-a)。继承可能导致类层次过于复杂,而组合则更加灵活,可以更自由地组合对象的功能...
Item 18: Favor composition over inheritance Item 19: Design and document for inheritance or else prohibit it Item 20: Prefer interfaces to abstract classes Item 21: Design interfaces for posterity ...
argue that..."和"But people who favor... maintain that..."对比正反两方的论据,强调了问题的复杂性,并提出解决建议。这样的论述方式有助于培养批判性思维和辩证分析问题的能力。 4. 原因-结果结构与疑问句式...
(WROX官方网站提供)What is this book about? ASP.NET 2 Beta Preview is timed ... Examples throughout the book do not favor one developer over another. Instead, every example is provided in both languages.
个人收藏 myfavor.php 酷帖排行 coolrank.php?number=7 #管理员设置酷帖(斑竹设置酷帖不在此列) 用户身份信息 showuserdetail.php 个人发帖 personarticle.php?number=8 #8可以...
创建Favor记录以及防止重复创建.mp4 课时69.在Forum页与View页显示喜欢数.mp4 课时70.在Header中显示个人主页按钮.mp4 课时71.Follow的实体与DAO接口.mp4 课时72.实现Follow关注功能.mp4 课时73.检查是否关注了...