Mixin

什么是Mixin?

In object-oriented programming languages, a Mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depends on the language. Mixins are sometimes described as being "included" rather than "inherited".

Mixins encourage code reuse and can be used to avoid the inheritance ambiguity that multiple inheritance can cause[1] (the "diamond problem"), or to work around lack of support for multiple inheritance in a language. A mixin can also be viewed as an interface with implemented methods. This pattern is an example of enforcing the dependency inversion principle.

之前一直以为是多高深的技术点,原来只是编码上的一种技巧,通过这个技巧避免了多继承中属性继承混淆的问题

Mixin的使用场景?

  1. 你希望给一个类提供很多可选的特征(feature).
  2. 你希望在很多不同的类中使用一个特定的特征(feature).

参考文档

https://www.cnblogs.com/aademeng/articles/7262513.html

results matching ""

    No results matching ""