Last active
November 21, 2018 13:59
-
-
Save graphicbeacon/d59ffab453c4dd43483852c3501bb98e to your computer and use it in GitHub Desktop.
Sample code demonstration Interfaces and Mixins in Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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