Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guidance for when to use inheritance vs composition #298

Open
plinss opened this issue Mar 30, 2021 · 2 comments · May be fixed by #504
Open

Guidance for when to use inheritance vs composition #298

plinss opened this issue Mar 30, 2021 · 2 comments · May be fixed by #504
Assignees

Comments

@plinss
Copy link
Member

plinss commented Mar 30, 2021

This was brought up in our discussion of w3ctag/design-reviews#587.

In the example spec, there's a question if the color select event should inherit from a PointerEvent, or should be a new event type that contains an optional reference to the pointer event.

This raises the general question, of when should we recommend composition vs inheritance. We should do some spelunking and look for existing patterns.

@martinthomson
Copy link
Contributor

https://en.wikipedia.org/wiki/Liskov_substitution_principle is a nice starting point for this.

@LeaVerou
Copy link
Member

LeaVerou commented Jun 3, 2024

I cannot find the discussion that prompted this, but generally inheritance is for "is a" relationships, while composition of the type described in the OP (a property pointing to another object) is for modeling all other types of relationships.
The Liskov substitution principle is a good test for this.

martinthomson added a commit that referenced this issue Jul 15, 2024
@martinthomson martinthomson linked a pull request Jul 15, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants