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:
No constructors can be in the class declaration.
The base class of the
mixin
must be the Object.No calls to a super class are made.
mixins
are really just classes that are malleable enough to fit into the class hierarchy at any point. A use case for a mixin...