Mixins in Dart

Yubaraj poudel
3 min readDec 30, 2019

--

Object-oriented programming is the best known for its reusability and easy to compare with the real world. In this topic, I tried to cover the reusability of the classes in Dart.

Let imagine we have two classes, each of them with their own behaviors and attributes. The most common solution is just to outline the attributes for the classes, but in real-world most classes are not unique wholly and share lots of common behavior.

Let's take an example of Aeroplane and Bus

class Aeroplane {
void vehicle() => print(“vehicle”);
void passenger() => print(“many passengers”);
void carryLoad() => print(“carry load”);
void detail() {
vehicle();
passenger();
carryLoad();
print(“Run in airport”);
}
}
class Bus {
void vehicle() => print(“vehicle”);
void passenger() => print(“many passengers”);
void carryLoad() => print(“carry load”);
void detail() {
vehicle();
passenger();
carryLoad();
print(“Run in road”);
}
}
void main() {
Aeroplane().detail();
Bus().detail();
}

Outputs would be

vehicle 
many passengers
carry load
Run in airport
vehicle
many passengers
carry load
Run in road

What should we do if we need to add another class Car? The most common method is to use the extension. Here we can make an abstract class Vehicle with all common methods and make them available to other classes by extending it. We basically make the parent class abstract to prevent making the object of it rather allow to inherit only.

abstract class Vehicle {
void vehicle() => print("vehicle");
void passenger() => print("many passengers");
void carryLoad() => print("carry load");
void detail(String detail) {
print("${this.runtimeType}-------");
vehicle();
passenger();
carryLoad();
print(detail);
}
}
class Aeroplane extends Vehicle {
}
class Bus extends Vehicle {
}
void main() {
Aeroplane().detail("Run in airport");
Bus().detail("Run in land");
}

Mixins

This is a nice approach as we can add more vehicles now. But one problem is as we increase the vehicle, we will realize all the methods aren't for just Vehicle. Let's take an example Ship, Instead of it runs it floats, which is very limiting when we need to add more functionalities since we can extend only one class. In this case, we need mixins.

mixin Run {
void run() => print("can Run");
}
mixin Fly{
void fly() => print("can Fly");
}
mixin Float{
void float() => print("float in water");
}
abstract class Vehicle {
void vehicle() => print("vehicle");
void passenger() => print("many passengers");
void carryLoad() => print("carry load");
}

class Aeroplane extends Vehicle with Fly {
detail() {
vehicle();
passenger();
carryLoad();
fly();
}

}
class Bus extends Vehicle with Run {
detail() {
vehicle();
passenger();
carryLoad();
run();
}

}
class Ship extends Vehicle with Float {
detail() {
vehicle();
passenger();
carryLoad();
float();
}
}
void main() {
Aeroplane().detail();
Bus().detail();
Ship().detail();
}

The last trick we have is the ability to do something that I like to think about as a reverse-extension. We can create a mixin that utilizes the methods from a class, which we can then use with each subclass.

If we want to break detail, we can do it by creating it with its own mixins using on a keyword to give access to only Vehicle class.

mixin Detail on Vehicle {
void detail(String action) {
print("${this.runtimeType}--------");
vehicle();
passenger();
carryLoad();
print(action);
}
}
abstract class Vehicle {
void vehicle() => print("vehicle");
void passenger() => print("many passengers");
void carryLoad() => print("carry load");
}

class Aeroplane extends Vehicle with Detail {
}class Bus extends Vehicle with Detail {

}
class Ship extends Vehicle with Detail {
}
void main() {
Aeroplane().detail("Can fly");
Bus().detail("Can Run");
Ship().detail("Can Float");
}

Conclusion

Flutter in a short period of time became significantly successful among developers with support of powerful Dart language. Hence it is good to learn about the functionalities of Dart. All the above codes are tested and can be run directly in https://dartpad.dartlang.org/ for learning purposes. I hope this will help you in understand the basics of Dart classes and reusability.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Yubaraj poudel
Yubaraj poudel

Written by Yubaraj poudel

Senior Software engineer | Blogger

No responses yet