Book Image

Dart By Example

By : David Mitchell
Book Image

Dart By Example

By: David Mitchell

Overview of this book

Table of Contents (17 chapters)
Dart By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 can be in the class declaration.

  2. The base class of the mixin must be the Object.

  3. 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...