When you want to "copy"/Expose the base class' API, you use inheritance. When you only want to "copy" functionality, use delegation.
One example of this: You want to create a Stack out of a List. Stack only has pop, push and peek. You shouldn't use inheritance given that you don't want push_back, push_front, removeAt, et al.-kind of functionality in a Stack.
They have nothing to do with each other. Delegation is a behavior. Inheritance is a model technique.
Inheritance is for modeling "is-a". A computer "is-a" electronic system.
Delegation is how methods provide results. Sometimes one object will delegate work to another object. Delegation can be via any relationship -- you can delegate to a superclass, to a member of a composite or aggregate, or any relationship.
You may use delegation to multiple internal class instances to simplify their functionality into a common grouping. If your language doesn't implement multiple inheritance for instance you may inherit from one of the bases and wrap the other, delegating the functionality you want to expose to the underlying implementation. Inheritance also ties your class into the hierarchy of classes you are inheriting from where as with delegation you may keep your place in your own hierarchy and delegate calls to another.
I agree with @Anzurio. Just to explain it in simple words:
Use inheritance only when you feel like the new class is a natural extension of the existing class. Prefer to use composition/delegation for all other purposes like using a specific method, etc.
In my opinion, delegation can be called when it's time to use and inheritance is embedded ever.
Take the in_threads gem for example, it use InThreads delegator to implement the threads for any Ruby Enumerable module. With this, only call in_threads methods on a array, it will run in threads.
发布评论
评论(6)
当您想要“复制”/公开基类的 API 时,可以使用继承。
当您只想“复制”功能时,请使用委派。
一个例子:
您想从列表中创建一个堆栈。 栈只有pop、push和peek三种。 鉴于您不希望在堆栈中使用push_back、push_front、removeAt 等功能,您不应该使用继承。
When you want to "copy"/Expose the base class' API, you use inheritance.
When you only want to "copy" functionality, use delegation.
One example of this:
You want to create a Stack out of a List. Stack only has pop, push and peek. You shouldn't use inheritance given that you don't want push_back, push_front, removeAt, et al.-kind of functionality in a Stack.
他们彼此没有任何关系。 委派是一种行为。 继承是一种模型技术。
继承是为了建模“is-a”。 计算机“是一个”电子系统。
委托是方法提供结果的方式。 有时,一个对象会将工作委托给另一个对象。 委派可以通过任何关系进行——您可以委派给超类、组合或聚合的成员或任何关系。
They have nothing to do with each other. Delegation is a behavior. Inheritance is a model technique.
Inheritance is for modeling "is-a". A computer "is-a" electronic system.
Delegation is how methods provide results. Sometimes one object will delegate work to another object. Delegation can be via any relationship -- you can delegate to a superclass, to a member of a composite or aggregate, or any relationship.
您可以使用多个内部类实例的委托来将它们的功能简化为公共分组。 例如,如果您的语言没有实现多重继承,您可以从其中一个基础继承并包装另一个基础,将您想要公开的功能委托给底层实现。 继承还将您的类与您继承的类的层次结构联系起来,就像使用委托一样,您可以在自己的层次结构中保留自己的位置并将调用委托给另一个类。
You may use delegation to multiple internal class instances to simplify their functionality into a common grouping. If your language doesn't implement multiple inheritance for instance you may inherit from one of the bases and wrap the other, delegating the functionality you want to expose to the underlying implementation. Inheritance also ties your class into the hierarchy of classes you are inheriting from where as with delegation you may keep your place in your own hierarchy and delegate calls to another.
假设您的类称为 B,并且派生/委托的类称为 A,那么
以下是使用继承或委托时的一些示例:
如果
Assume your class is called B and the derived/delegated to class is called A then
Here are some examples when inheritance or delegation are being used:
If
我同意@Anzurio。 用简单的话来解释一下:
I agree with @Anzurio. Just to explain it in simple words:
在我看来,当需要使用时可以调用委托,并且继承是嵌入的。
以 in_threads gem 为例,它使用 InThreads 委托器来实现任何 Ruby Enumerable 模块的线程。 这样,只需在数组上调用 in_threads 方法,它将在线程中运行。
In my opinion, delegation can be called when it's time to use and inheritance is embedded ever.
Take the in_threads gem for example, it use InThreads delegator to implement the threads for any Ruby Enumerable module. With this, only call in_threads methods on a array, it will run in threads.