O'Reilly logo

Dart By Example by Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mixin' it up

In object-oriented languages, it is useful to build on one class to create a more specialized related class. For example, in the text editor, the base dialog class was extended to create an alert and confirm pop ups. What if we want to share some functionality but do not want inheritance occurring between the classes?

Aggregation can solve this problem to some extent:

class A {
  classb usefulObject;
}

The downside is that this requires a longer reference to use:

new A().usefulObject.handyMethod();

This problem has been solved in Dart (and other languages) by having a mixin class do this job, allowing the sharing of functionality without forced inheritance or clunky aggregation.

In Dart, a mixin must meet the following requirements:

  1. No constructors ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required