logoalt Hacker News

flohofwoelast Wednesday at 8:58 AM1 replyview on HN

Yes there is some inheritance left in those composition-systems, but a single inheritance level is really more like implementing an interface.


Replies

llmslave2last Wednesday at 9:02 AM

It is, but you get the benefit of shared behaviour and state.

Like an Entity might have a Position, a reference to the World, the Screen, methods for interacting with other entities, etc. You don't get that from simply implementing an interface, although it's not difficult to pass those into the object. A common example I've seen is having every class extend an EventEmitter superclass. You could implement that as part of the interface but that becomes a ton of duplication.

I think of it like this: If you model your domain as something like `A : B : C : D {}` you get all the problems of inheritance, when simply doing D { A; B; C; } gives you the same benefits without the problems. Doing `A : X {}, B : X {}, C : X {}` sidesteps most of the problems with inheritance but gives you some of the benefits as well.