This series documents the learning path of Flutter from scratch, starting with Dart grammar. Mixin is a dart specific syntax, and this article describes its usage scenarios and usage in detail.
primers
Consider a scenario where Xiao Ming and Xiao Fang are both programmers. They can dance, and of course they can program.
The object-oriented approach can be modeled as follows:
Since Xiao Ming and Xiao Fang can write programming, in order to reuse this behavior, we extracted the superclass Programmer, which contains the behavior code() common to all programmers. In this way, Ming and Fang can reuse programming behavior rather than each reimplementing the same logic. (Inheritance reuses behavior)
Xiaohui is a dancer, then modeled using an object-oriented method as follows:
Such inheritance violates the DRY principle, or Don’t Repeat Yourself. (An example of the DRY principle can be found in how to make good use of polymorphism to write smelly, long, and hard to maintain code.)
The same dance logic appears twice because Hui does not reuse Fang’s dancing behavior.
Does that solve the problem by referring dancing behaviors to their common base class, Human? True, but doesn’t that force all programmers to be able to dance…
Can having Xiao Fang inherit both Programmer and Dancer solve the problem? Can!!!! But multiple inheritance can cause problems, like the “Diamond Problem” :
If we have the eat() method in the Human class and both Programmer and Dancer override it, Fang will compile an error. Because it doesn’t know which superclass implementation to use for its eat() method. The class diagram above is called Diamond problem because it looks like a Diamond shape.
Dart disables multiple inheritance and instead introduces mixins to address this issue.
Mixins are special classes whose properties and behaviors can be reused by other classes without inheritance.
grammar
Mixins are used when you want a set of attributes and behaviors that can be reused for multiple classes that happen not to be on the same inheritance link:
mixin DanceMixin {
void dance() {}
}
Copy the code
This is the way to declare mixins, almost exactly the same way to declare classes, except that class is replaced with mixins.
Mixins can also be scoped with on:
mixin DanceMixin on Human {
void dance() {// The dance logic is implemented here}
}
Copy the code
Thus DanceMixin can only be used for the Human class.
Mixin can also override the methods of the Human class:
class Human {
void eat() {}
}
mixin DanceMixin on Human {
void dance() {}
@override
void eat() {
super(a)... }}Copy the code
Fang and Hui used mixin to reconstruct the following:
class Fang extends Programmer with DanceMixin {} // Fang reuses dance logic
class Hui extends Human with DanceMixin {} // Reuse dance logic
Copy the code
The keyword with denotes the use of mixins, and a class can use multiple mixins at the same time, separated by,.
reference
Multiple Inheritance in C++ and the Diamond Problem (freecodecamp.org)
Language tour | Dart