Flyter – DART’s mixin

Time:2021-4-13

The original text is inhere. Well written, I recommend you to read the original.

Here is the definition of mixin
As long as a class is inherited fromObjectIf there is no constructor defined, then this class can be a mixin. Of course, if you want to make the definition of mixin clearer, you can usemixinKeyword. Please refer tohere

The original screenshot to experience the style.

Flyter - DART's mixin

text

In the classic object-oriented programming language, there must beRegular classesabstract classandInterface. Of course, dart has its own interface, but that’s another article. Sometimes other beasts lurk in the shadows:MixinWhat is this for and how to use it? Let’s find out together.

A world without mixin

Suppose you’re building an app that simulates wildlife, then you need oneMosquitoMosquitoes. As a visionary developer, you willabstractWhat mosquitoes have in common and then put it in one placeabstract classIn.

abstract class Insect {
  void crawl() {
    print('crawling');
  }
}

abstract class AirborneInsect extends Insect {
  void flutter() {
    print('fluttering');
  }

  void buzz() {
    print('buzzing annoyingly')
  }
}

class Mosquito extends AirborneInsect {
  void doMosquitoThing() {
    crawl();
    flutter();
    buzz();
    print('sucking blood');
  }
}

excellent! You’ve done it! Adding new insects is like a breeze. There is no redundancy at all… Until you find out you need oneSwallowClass (that is, something that can eat the whole mosquito).

There are also many things that birds have in common. We can create oneBirdClass. This is when the problem arises – birds can also vibrate their wings! But you can’tflutterMethods fromAirboneInsectClass to form a new classFluttering

Why?BirdClass can be inheritedFlutteringClass, butAirboneInsectNo, it’s inheritedInsectClass. Dart doesn’t support itMultiple inheritanceIt’s really good.

Now, you need to giveBirdClass to add aflutterThere’s no way. Code redundancy happened!

abstract class Bird {
  void chirp() {
    print('chirp chirp');
  }

  // Duplicate method
  void flutter() {
    print('fluttering');
  }
}

class Swallow extends Bird {
  void doSwallowThing() {
    chirp();
    flutter();
    print('eating a mosquito');
  }
}

Using mixin

Mixin is defined as “a method to use class code in multiple inheritance trees.”. In short, mixin allows you to introduce code block methods without inheritance. To declare a mixin is very simple:

mixin Fluttering {
    void flutter() {
        pring('fluttering');
    }
}

thismixinIt can only be used in a general abstract classwithkeyword. In the wild animal simulation app example, you may want to use it on abstract classes.

mixin Fluttering {
  void flutter() {
    print('fluttering');
  }
}

abstract class Insect {
  void crawl() {
    print('crawling');
  }
}

abstract class AirborneInsect extends Insect with Fluttering {
  void buzz() {
    print('buzzing annoyingly');
  }
}

class Mosquito extends AirborneInsect {
  void doMosquitoThing() {
    crawl();
    flutter();
    buzz();
    print('sucking blood');
  }
}

abstract class Bird with Fluttering {
  void chirp() {
    print('chirp chirp');
  }
}

class Swallow extends Bird {
  void doSwallowThing() {
    chirp();
    flutter();
    print('eating a mosquito');
  }
}

You can also use multiple mixins on a single class. If you need to use achirpingIn case of mixn, just use it with flying mixin.

abstract class Bird with Fluttering, Chirping

Limiting the use of mixin

Sometimes you don’t want mixin to be used on existing classes, you just want it to be used on certain classes or subclasses. Usually your code depends on existing code.

In the case of the wildlife app, you find that only supportswallowIt’s not enough. There are other kinds of birds, such as sparrow or blue jay. Unlike sparrows, many other birds need to peck from the ground, seeds, insects…

toBirdAdding methods to abstract classes is impossible because not all birds need to peck. So, to avoid code redundancy, you can create aPeckingMixin.

mixin Pecking {
  void peck() {
    print('pecking');
  }
}

That’s at least a lot better than code honor. However, some people will use this mixin in the futureInsectClass. In this way, using mixin is a big problem.

When you start to study the movement of a bird, you will find that the bird will emit a pleasant song after the food is expelled. Call from the current mixinchirpThe method is impossible. It’s very easy to change. Just tell dart,PeckingMixin can only be used inBirdClass. Now callBirdThere is no problem with the methods of the class.

//...
mixin Pecking on Bird {
    void peck() {
        print('pecking');
        chirp();
    }
}

class Sparrow extends Bird with Pecking {}

class BlueJay extends Bird with Pecking {}

conclusion

Mixin is very useful for avoiding code redundancy in inheritance systems. Mixin can also be constrained to be used only for certain classes, which makes them a powerful tool for development!

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]