Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Last active November 21, 2018 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphicbeacon/d59ffab453c4dd43483852c3501bb98e to your computer and use it in GitHub Desktop.
Save graphicbeacon/d59ffab453c4dd43483852c3501bb98e to your computer and use it in GitHub Desktop.
Sample code demonstration Interfaces and Mixins in Dart
void main() {
var pixel = Phone('Pixel XL', 'Google');
pixel.getDeviceInfo();
pixel.getAllFeatures();
}
class FeaturesMixin {
bool blueTooth = true;
bool dualSim = false;
bool NFC = true;
}
mixin UtilitiesMixin on FeaturesMixin {
bool flashlight = true;
bool calculator = true;
bool thermometer = false;
String _has(bool feat) => feat ? 'Yes' : 'No';
void getAllFeatures() => print('''
-- FEATURES --
BlueTooth: ${_has(super.blueTooth)}
Dual SIM: ${_has(super.dualSim)}
NFC: ${_has(super.NFC)}
Flashlight: ${_has(flashlight)}
Calculator: ${_has(calculator)}
Thermometer: ${_has(thermometer)}
===
''');
}
abstract class Device {
String model;
String manufacturer;
void getDeviceInfo();
}
class Phone with FeaturesMixin, UtilitiesMixin implements Device {
String model;
String manufacturer;
Phone(this.model, this.manufacturer);
void getDeviceInfo() => print('''
===
Device name: $model
Manufactured by: $manufacturer
''');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment