What is the difference between an Abstract Class and a Mixin?

前端 未结 2 1076
暗喜
暗喜 2021-02-04 06:12

I just found an article on a framework in Java that apparently allows it to support Mixins and something called Composite Oriented Programming (which for all I know might even b

相关标签:
2条回答
  • 2021-02-04 06:51

    At a language-agnostic level, a mixin just adds functionality to a class, and is more for programmer convenience and to avoid code duplication. An abstract (base) class forms an is-a relationship and allows for polymorphism. One reason why inheritance is overused is that it's an easy way to implement mixins without writing any boilerplate in languages that don't really support them. The problem is that you're declaring a polymorphic is-a relationship as a side effect, making your API more confusing and possibly adding ambiguity. Hence, newer languages like D and Ruby support mixins as native features, allowing a convenient way to add a bunch of functionality to a class without declaring a polymorphic is-a relationship.

    0 讨论(0)
  • 2021-02-04 07:00

    Mixin is never meant as stand alone class. They just add some functionality to the class you declare. In Python they can be easily applied by class decorators. For example you could decorate your class with Singleton mixin, making your class a singleton.

    0 讨论(0)
提交回复
热议问题