Mixin class mixin in Dart

introduce

Mixin is a method pattern for reusing code in a class in multiple inheritance.

Mixins are classes in object-oriented programming languages ​​that provide method implementations . Other classes can access the methods and variablesmixin of the class without being a subclass of it .

To put it simply, the official design is a class that can be reused easily, without having to implement many interfaces.

Application Scenario

 For example, there is a superclass of Anmal, and there are three subclasses below it: Mammal, Bird, and Fish, and there are more subclasses under it.

Here we take three classes as examples, namely Duck, Cat, and Dove. Among them, Duck can do all three methods, Cat can walk, and Dove can walk and fly. If you want to use inheritance to implement it, it is as follows:

abstract class Animal{}
abstract class Swim{
  void swim();
}
abstract class Walk{
  void walk();
}
abstract class Fly{
  void fly();
}

class Duck extends Animal implements Swim,Walk,Fly{
  @override
  void fly() => print("fly");

  @override
  void swim() => print("swim");

  @override
  void walk() => print("walk");

}
class Cat extends Animal implements Walk{
  @override
  void walk() => print("walk");
}
class Dove extends Animal implements Walk,Fly{
  @override
  void fly() => print("fly");

  @override
  void walk() => print("walk");
}
void main(){
  Duck().swim();
  Duck().walk();
  Duck().fly();
  Cat().walk();
  Dove().fly();
  Dove().walk();
}

The output results are as follows:

 Because dart is also a single inheritance language, this usage is exactly the same as in java, but it is very redundant to rewrite all the methods in the interface. Is there a way that we can reuse the methods in the interface? At this time You can use the mixin.

We use mixin instead of interface. If you want to reuse the method of mixin class, you only need to add the with keyword after the class, as follows:

abstract class Animal{}

mixin Swim{
  void swim() => print("swim");
}
mixin Walk{
  void walk() => print("walk");
}
mixin Fly{
  void fly() => print("fly");
}

class Duck extends Animal with Swim,Walk,Fly{}
class Cat extends Animal with Walk{}
class Dove extends Animal with Walk,Fly{}

void main(){
  Duck().swim();
  Duck().walk();
  Duck().fly();
  Cat().walk();
  Dove().fly();
  Dove().walk();
}

The final printed result is exactly the same as above, and the method to implement the special case in the class is the same as the method of using the interface, just rewrite it directly.

Advanced usage

Linearization properties of mixins

If the same method exists in the class we mix in, how will it be called? For example, the run() method is defined in both the Person class and the A class, let's call it in the Male class

class Person{ run() => print("person"); }

mixin A{ run() => print("A"); }

class Male extends Person with A{}

main(){
  Male().run();
}

The result is as follows:

-------------------------------------------------------------------------------------------------------

 

A

 Here only the run() method in class A is executed. In dart, there is a grammatical feature called linearization

 The code for the above mixin is equivalent to

Class PersonA = Person with A
Class Male extends PersonA;

 It is equivalent to the run() method in the Person class being rewritten by A, so the rewritten run() method in the A class is called.

Then we have a way to call the method in the Person class? The answer is of course yes, and it is very simple. Since it is inheritance, we can directly call the parent method, plus super.run()

class Person{ run() => print("person"); }

mixin A{
  @override
  run() {
    super.run();
    print("A");
  }
}

class Male extends Person with A{}

main(){
  Male().run();
}

The output is:

person
A

on

When using mixin, you can use the on keyword to restrict. When mixing in this class, you must first inherit or mix in the class behind on

like:

class Person{ run() => print("person"); }

mixin A{ run() => print("A"); }

mixin C on Person{
  @override
  run() {
    super.run();
    print("C");
  }
}

//这里还可以是
//class Femal extends Person with A,C{}

class Female with A,Person,C{}  //混入时,Person类必须在C之前

main(){
  Female().run();
}

final output

person
C

reference

Mixin analysis in dart - Attack on Summer's Blog - CSDN Blog - dart mixin

In-depth understanding of Mixin in Dart - Programmer Sought

Guess you like

Origin blog.csdn.net/TDSSS/article/details/129123887