Skip to content

Instantly share code, notes, and snippets.

@eernstg
Last active November 4, 2019 14:45
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 eernstg/bddf299bff9ef5ef1331a4ab9329e60f to your computer and use it in GitHub Desktop.
Save eernstg/bddf299bff9ef5ef1331a4ab9329e60f to your computer and use it in GitHub Desktop.
Code for comment on 'How language features affect OO design'
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