Using mixins

In Dart, just like in Ruby, your classes can use mixins to assign a certain behavior to your class. Say an object must be able to store itself, so its class mixes in a class called Persistable that defines save() and load() methods. From then on, the original class can freely use the mixed-in methods. The mechanism is not used for specialized subclassing or is-a relationships, so it doesn't use inheritance. This is good because Dart uses a single inheritance, so you want to choose your unique direct superclass with care.

How to do it...

  • Look at the mixins project; the Embrace class from the previous recipe needs to persist itself, so it mixes with the abstract class Persistable, thereby injecting the save and load behavior. Then, we can apply the save() method to the embr object, thereby executing the code of the mixin as follows:
    void main() {
      var embr = new Embrace(5);
  • Using the mixins methods, as shown in the following code:
      print(embr.save(embr.strength));
      print(embr is Movement); // true
      print(embr is Persistable); // true 
    }
  • Mixing in Persistable, as shown in the following code:
    class Embrace extends Movement with Persistable {
      // code omitted, see previous recipe 
    }
    
    class Movement {
      String name;
      Movement();
     }
  • Defining Persistable, as shown in the following code:
    abstract class Persistable {
      save(var s) {
        // saving in data store
        return "You are saved with strength $s!";
      }
      load() => "You are loaded!";
    }

The previous program produces the output You are saved with strength 5!

How it works...

The abstract class Persistable is mixed in with the keyword with. The class Embrace is a subclass of class Movement, Embrace is Movement; if you don't have a direct superclass, as shown in the following code:

class Embrace extends Object with Persistable

So it means that in order to use a mixin, you will always need to extend a class. Objects of the class Embrace are also of the mixed-in type Persistable.

There's more...

The class that is mixed in is usually an abstract class, but it doesn't have to be. You can also mix in several classes to give a taste of multiple inheritance, as shown in the following code:

class Developer extends Person with Intellectual, Addicted

The mixed-in class has to obey some restrictions as follows:

  • It must not declare a constructor
  • Its superclass is an object
  • It may not contain calls to the superclass

The mixin concept very much resembles the implementation of an interface mechanism, which exists in many other languages. However, it is much more powerful because the mixed in class(es) can contain real code that can be executed, whereas interfaces can't contain code; only definitions of methods. So that's why using mixins is a good practice.

Tip

Always examine your inheritance relationship to see if it can be better described by a mixin than with a superclass.

In Dart, you can also use the implements keyword; a class can implement one or more other classes. What's the difference between implementing and mixing in? Implementing means that the class provides its own code for the public methods from the class it implements, while mixing in means that the class can use the code from the mixin class itself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset