TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Why composition is often better than inheritance

192 点作者 exch将近 11 年前

19 条评论

al2o3cr将近 11 年前
Meh. I think this phrase has been repeated until it has lost any connection with the original intent and turned into a generic &quot;INHERITANCE BAD! COMPOSITION GOOD!&quot; without much meaning attached to either word.<p>I haven&#x27;t found the original source, but I&#x27;ve always presumed the statement originally referred to some of the bizarro &quot;inheritance-as-composition&quot; stuff in the early C++ days: for instance, you might have a class &#x27;Window&#x27; and a class &#x27;Button&#x27;, then combine them with multiple inheritance to get a &#x27;WindowWithButton&#x27;, then inherit from <i>that</i> and a &#x27;Scrollbar&#x27; class to get &#x27;WindowWithButtonAndScrollbar&#x27;.<p>I can&#x27;t imagine anybody thinking of that as a &quot;good&quot; pattern today, but remember it was the &#x27;90s. :)<p>Nowadays, the basic statement has been dogmatized to the point where you get code like this:<p><a href="https://github.com/elm-city-craftworks/broken_record/blob/master/lib/broken_record/composable.rb" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;elm-city-craftworks&#x2F;broken_record&#x2F;blob&#x2F;ma...</a><p>This code re-implements Ruby&#x27;s built-in method lookup algorithm, but with per-instance objects and none of the optimizations available to the real thing. It basically remakes inheritance, slowly and poorly, using composition.<p>The other one that makes me scratch my head: people who rail against inheritance, then suggest mixins as an alternative. At least in Ruby, the two are <i>equivalent</i>. Check the `ancestors` property on a class with mixins sometime if you don&#x27;t believe me.<p>TL;DR (too late) - use your damn brain to make decisions, not just parrot slogans.
评论 #7996256 未加载
评论 #7995197 未加载
userbinator将近 11 年前
While it&#x27;s possible to overuse inheritance, I don&#x27;t think replacing it with composition is all that much better, and in addition all those forwarding methods that do nothing more than call another (could they even be optimised out?) are a great example of code that would need to be written, consuming resources like programmer time, but otherwise serves no true useful purpose to the functionality of the software. The complexity only changes form, so instead of tracing the flow through an inheritance hierarchy you&#x27;re just doing it through chains of forwarding methods. It&#x27;s for this same reason I don&#x27;t believe so much his argument for readability and short classes - breaking everything up does not make things simpler, it makes the complexity spread out over a larger area; while it may be true that it is easier to understand an individual piece, it becomes more difficult to understand the system as a whole. This is especially important when debugging, where &quot;can&#x27;t see the forest for the trees&quot; is a big hindrance.<p>I think his example of flexibility is the strongest argument for composition, because in that case the forwarding methods are not a waste - they would need to do (useful) work to determine which of the multiple composited objects they would need to work with.<p>Being mostly a C programmer who does OO-things, I use inheritance when it&#x27;s obvious that most of the &quot;methods&quot; will be passthroughs to the &quot;superclass&quot;, and composition when there is something more that needs to be done. Also, as I am not constrained by the OO model&#x2F;conventions of the language, it&#x27;s more flexible in that I can do things like &quot;inherit&quot; multiple times and even change that at runtime, so there is really no strict separation between composition and inheritance; to me, it&#x27;s just &quot;which function do I set this to point to.&quot;
评论 #7996129 未加载
评论 #7995450 未加载
评论 #7994571 未加载
chton将近 11 年前
While it&#x27;s a well-written article, it really seems like beating a dead horse. Composition over inheritance is a basic rule of OO programming, so much so that it has its own wikipedia page (<a href="http://en.wikipedia.org/wiki/Composition_over_inheritance" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Composition_over_inheritance</a>)
评论 #7994627 未加载
评论 #7994410 未加载
评论 #7994424 未加载
评论 #7994413 未加载
评论 #7994705 未加载
评论 #7994671 未加载
kissgyorgy将近 11 年前
In Python, we use mixins. Mixins can <i>only</i> inherit from &#x27;object&#x27; and nothing else, like this:<p><pre><code> class PhysicsobjectMixin(object): def update_physics(self): pass def apply_konckback(self, force): pass def get_position(self): pass class FightMixin(object): def attack(self): pass def defend(self): pass class TalkMixin(object): def say_something(self): pass class Character(PhysicsobjectMixin, FightMixin, TalkMixin): pass class Pickup(PhysicsobjectMixin): pass class Projectile(PhysicsobjectMixin): pass </code></pre> it&#x27;s still inheritance, but the classes will be flat; every class only inherits one deep, so there will be no diamond problems and no repeating code.
评论 #7994616 未加载
评论 #7994847 未加载
评论 #7995776 未加载
评论 #7994519 未加载
评论 #7994509 未加载
millstone将近 11 年前
Consider how these things get stored. In the inheritance model, you might have a big quad tree or some other data structure of PhysicsObjects, and just run through and call updatePhysics() on all of them.<p>In the composition model, we now have multiple classes (Character, Pickup, Projectile), each with an <i>unrelated</i> updatePhysics(). This means code duplication to call the relevant method on each separate class.<p>We could relate them all via an interface, instead of inheritance; now we can store IEntity or whatever. We will soon discover three needs that are awkward to address:<p>1. Whenever we want to add some new method (say `fall`), we must go back and implement it separately in each class.<p>2. Different classes will want to share implementations. For example, both Characters and Pickups bounce on fall.<p>3. Some classes will want to specialize an implementation to do more. Characters bounce on fall, but also take damage.<p>In practice you may end up with both: an interface that your engine talks to, but also a common base class that provides sane defaults.<p>So while interfaces allow uniform interactions with disparate classes, inheritance provides that and also the ability to share and specialize the implementations. So inheritance solves some problems that interfaces cannot.<p>See also default methods in Java, which makes an interface more like a class, and implementing an interface more like inheritance. The documentation even says that a class that implements an interface inherits its default methods.
评论 #7996337 未加载
jiaweihli将近 11 年前
I think this is a tooling issue. People initially tend to favor inheritance because it looks cleaner than composition. Mixing a lot of unrelated code in the same class makes things hard to find. (which method applies to which composed object?)<p>In languages that build in a concept of traits&#x2F;mixins however, this isn&#x27;t an issue.
评论 #7994605 未加载
评论 #7994422 未加载
taeric将近 11 年前
I would prefer this with &quot;why a shallower abstraction pool is better than a deep one.&quot; I&#x27;ve seen some compositional concoctions that were just as terrible to deal with as inheritance based ones. I think I&#x27;ve even contributed&#x2F;originated some.
zak_mc_kracken将近 11 年前
We&#x27;ve known this since at least 1994, when the GoF book [1] famously said:<p>&quot;Favor object composition over class inheritance&quot;<p>[1] <a href="http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612" rel="nofollow">http:&#x2F;&#x2F;www.amazon.com&#x2F;Design-Patterns-Elements-Reusable-Obje...</a>
评论 #7994863 未加载
pllbnk将近 11 年前
When I try to choose between the two, I often like to think if the object I try to inherit from is from the same domain&#x2F;context and solves a related problem. In the example in the article a PhysicsObject solves the problem of calculating coordinates in space and from the beginning it was not designed as something to be used in the game by itself. While the character participates in the actual game and executes the game logic. The character does not &#x27;inherit&#x27; from PhysicsObject, it merely knows that PhysicsObject represents it in the space.
teamhappy将近 11 年前
It&#x27;s pretty much the same example I used here a couple of days ago: <a href="https://news.ycombinator.com/item?id=7976227" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=7976227</a><p>Game development seems to be the poster child for composition over inheritance.<p>Here&#x27;s a lengthy article that explains it way better (IMHO): <a href="http://gameprogrammingpatterns.com/component.html" rel="nofollow">http:&#x2F;&#x2F;gameprogrammingpatterns.com&#x2F;component.html</a>
kilemensi将近 11 年前
I think one of the biggest reasons why most libraries&#x2F;frameworks&#x2F;apps&#x2F;etc. use inheritance over composition is the easy with which the underlying languages allow the use of inheritance as opposed to composition. Most of these software writers know SOLID and other OO principles but when they&#x27;re faced with the actual implementation, inheritance is just too damned easy to implement.
andybak将近 11 年前
The Wikipedia article chton mentions (<a href="https://en.wikipedia.org/wiki/Composition_over_inheritance" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Composition_over_inheritance</a>) ends with the following when discussing the drawback of composition (boilerplate for forwarding methods): &quot;This drawback can be avoided by using traits or mixins.&quot;<p>Now this is where things get a little blurry for me.<p>Mixins can help with the main drawback of Composition - but Mixins ARE inheritance - so isn&#x27;t this a contradiction?<p>If I use PhysicsObjectMixin in my CharacterComposition class then I have to inherit from it. So aren&#x27;t we back with the perils of inheritance?
评论 #7994440 未加载
kstenerud将近 11 年前
In this case I&#x27;d argue that the roles are a bit messy and Character has too much knowledge. Character should not know that physics objects can be updated, and certainly shouldn&#x27;t be calling updatePhysics. You could end up with an updated Character interacting with a Character whose physics state hasn&#x27;t been updated yet.<p>applyKnockback: Character -&gt; Physics object -&gt; Physics engine<p>updatePhysics: Physics engine -&gt; Physics object -&gt; Character new position (x, y)<p>updateCharacter: Character reacts to change
yayitswei将近 11 年前
By the way, I encourage everyone to try out their game, Awesomenauts. Think Super Smash Brothers meets Dota. It&#x27;s a lot of fun to play!
javinpaul将近 11 年前
Couldn&#x27;t agree more than this. I have also shared my 2 cents on Why composition is better than Inheritance for Java Programmers here <a href="http://javarevisited.blogspot.sg/2013/06/why-favor-composition-over-inheritance-java-oops-design.html" rel="nofollow">http:&#x2F;&#x2F;javarevisited.blogspot.sg&#x2F;2013&#x2F;06&#x2F;why-favor-compositi...</a>
_pmf_将近 11 年前
The burden of proof should fall onto the user of inheritance to justify his decision. Interfaces plug delegates is more tedious to implement, but greatly reduces the chances of an architecture turning into a complete train wreck.<p>I often wonder why declarative delagating is not a first class concern in programming languages.
yeureka将近 11 年前
In my opinion the best arguments against inheritance were written by Richard Gabriel in his book Patterns of Software: <a href="http://dreamsongs.net/Files/PatternsOfSoftware.pdf" rel="nofollow">http:&#x2F;&#x2F;dreamsongs.net&#x2F;Files&#x2F;PatternsOfSoftware.pdf</a>
known将近 11 年前
Sounds like <a href="https://en.m.wikipedia.org/wiki/Triarchy_%28theory%29" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Triarchy_%28theory%29</a>
fithisux将近 11 年前
Why often and not always? Can you provide an exceptional case?
评论 #7994822 未加载