Mixin vs inheritance

前端 未结 9 1009
梦毁少年i
梦毁少年i 2020-12-02 07:22

What is the difference between a mixin and inheritance?

9条回答
  •  孤城傲影
    2020-12-02 08:10

    Mixins are vastly used in a more "plugin" like manner.

    They are the same but in a different context each one of them. Usually when we talk about inheritance we are talking about SINGLE inheritance, and a mixin is a construct that allows MULTIPLE inheritance.

    This is a language construct that is highly controversial in the OOP world because of:

    • The ambiguity that it must be resolved
    • A lot of the time "mixin" classes don't work on its own, and may conflict with other mixins
    • It can result in a "diamond inheritance problem", where two super classes can inherit from the same class

    But that aside, is a powerful construct that's used in various languages and frameworks, some examples are:

    • Django
      • https://github.com/django/django/blob/98126cdfaf632abc8f0b5e65910e46a4eedc4641/django/views/generic/list.py#L194

      • https://docs.djangoproject.com/en/3.1/topics/class-based-views/mixins/

    • Typescript
      • https://www.typescriptlang.org/docs/handbook/mixins.html
      • https://vincit.github.io/objection.js/guide/plugins.html#_3rd-party-plugins

提交回复
热议问题