设计模式之 Putting Reuse Mechanisms to Work and Inheritance versus Composition


Most people can understand concepts like objects, interfaces, classes, and inheritance. The challenge lies in applyingthem to build flexible, reusable software, and design patterns can show you how.

很多人可以理解对象,接口,类和继承的概念。挑战是如何应用它们来建立灵活、重用的软件,和设计模式可以展示给你什么。

Inheritance versus Composition  继承与组合

The two most common techniques for reusing functionality in object-oriented systems are class inheritance and objectcomposition. 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 "white-box" refers to visibility: With inheritance, theinternals 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 atcompile-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 overriddenoperations.

继承和组合每一个都有它的优势和不利。自从它被程序语言所支持以来,类的继承时在编译时静态的定义的并且可以直截了当的用。在重用时,类的继承可以很容易的被修改。

当子类覆盖一些而不是全部的运算时,它也可以影响在继承时的运算,获得一定的权限它可以调用被覆盖的运算。

But class inheritance has somedisadvantages, too. First, you can't change the implementations inherited from parentclasses at run-time, because inheritance is defined at compile-time. Second, andgenerally worse, parent classes often define at least part of their subclasses'physical representation. Because inheritance exposes a subclass to detailsof its parent's implementation, it's

often said that "inheritance break sencapsulation" [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 theinherited 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 requirescarefully 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 theirinterfaces, we don't break encapsulation. Any

object can be replaced at run-time 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 in to 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.

在系统设计是,对象的组合还有其他的影响。支持组合超过了类的继承会帮助你保持类的封装和专注某个任务。你的类和类的继承会保持很小,几乎不可能成长为无法控制的怪物。另一方面,基于对象组合的设计会产生很多的对象,系统的行为会依赖它们的继承关系而不是被定义的一个类。

That leads us to our second principle ofobject-oriented design:Favor object composition over class inheritance.

这些导致了面向对象设计的第二个原则:支持对象的组合超过了类的继承。

Ideally, you shouldn't have to create new components to achieve reuse. You should be able to get all the functionality you need just by assembling existing components through object composition. But this is rarely the case, because the set of available components is never quite rich enough in practice. Reuse by inheritance makes it easier to make new components that can be composed with old ones. Inheritance and object composition thus work together.

理论上说,不需要创建新的构建来完成重用。你仅仅需要在对象的最合适装配现有的构件你就有能力获得你需要的所有的功能。但是这是一个罕见的案例,因为在实践中一套实用的构件是不会满足需要的。通过继承可以很容易的组合旧有的构件来组成新的构件。继承和组合紧紧联系在一起。

Nevertheless, our experience is that designers overuse inheritance as a reuse technique, and designs are often made more reusable (and simpler) by depending more on object composition. You'll see object composition applied again and again in the design patterns.

尽管如此,在重用技术中,在设计时我们使用继承的经验,在设计中我们经常通过依赖更多的对象的组合来使用重用。在设计模式中,你会看到对象的组合被应用了一遍又一遍。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值