Last active
November 4, 2019 14:45
-
-
Save eernstg/bddf299bff9ef5ef1331a4ab9329e60f to your computer and use it in GitHub Desktop.
Code for comment on 'How language features affect OO design'
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
main() { | |
new Dog().feed("some food"); | |
} | |
abstract class Pet { | |
void feed(String food); | |
void pet(); | |
} | |
class Dog = Pet with FeedableDog, PettableDog; | |
class Cat = Pet with FeedableCat, PettableCat; | |
mixin FeedableDog on Pet { | |
void feed(String food) => print(food + "? For me?"); | |
} | |
mixin FeedableCat on Pet { | |
void feed(String food) => print(food + "? Of course."); | |
} | |
mixin PettableDog on Pet { | |
void pet() => print("Yay!"); | |
} | |
mixin PettableCat on Pet { | |
void pet() => print("I'll allow it."); | |
} | |
// Of course, we could define interfaces `PettablePet`, | |
// `FeedablePet`, and add `implements PettablePet` etc. | |
// on the mixins. That would allow for more abstract code | |
// when each feature is accessed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment