Created
May 19, 2020 18:31
-
-
Save refi64/486a6dc9e28bcf3fa9aad369c3d8d137 to your computer and use it in GitHub Desktop.
Dart "protected methods" via interfaces & mixins
This file contains hidden or 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
abstract class MyType { | |
void doStuff(); | |
} | |
mixin MyTypeHelpers { | |
void protectedUtilityMethod() => print('Protected method'); | |
} | |
abstract class MyDerivedType implements MyType { | |
factory MyDerivedType.create() => MyDerivedTypeImpl(); | |
void doMoreStuff(); | |
} | |
class MyDerivedTypeImpl with MyTypeHelpers implements MyDerivedType { | |
void doStuff() { | |
print('Doing stuff'); | |
protectedUtilityMethod(); | |
} | |
void doMoreStuff() { | |
print('Doing more stuff'); | |
protectedUtilityMethod(); | |
} | |
} | |
void main() { | |
var value = MyDerivedType.create(); | |
value.doStuff(); | |
value.doMoreStuff(); | |
// Calling value.protectedUtilityMethod is a compile-time error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment