0

my Goal is to structure my code with this. The Idea is to outsource an inplementation to elsewhere, so that your main class is not so overloaded with way to much methods. Mixins seamed to be perfect for that! Example:

abstract class Animal {
  String printAllPropeties();
}

class Elephant extends Animal {
  Trunk trunk;   // Trunk: some Datastructure
  
  @override
  String printAllPropeties() => trunk.toString();
}

class Bird extends Animal {
  Wing wing;   // Wing: some Datastrukture
  
  @override
  String printAllPropeties() => wing.toString();
}

But now I want to outsource printAllPropeties() to a mixin for each class:

abstract class Animal {
  String printAllPropeties();
}

class Elephant extends Animal with ElephantAllPropeties {
  Trunk trunk;   // Trunk: some Datastructure
}

mixin ElephantAllPropeties on Elephant {
  @override
  String printAllPropeties() => trunk.toString();
}

class Bird extends Animal with BirdAllPropeties {
  Wing wing;   // Wing: some Datastrukture
}

mixin BirdAllPropeties on Bird {
  @override
  String printAllPropeties() => wing.toString();
}

But this would not work like it is now, because the mixin would implement itself. So is there another way, or is it simply not possible, or possible with other methods??

puaaaal
  • 196
  • 8
  • ```But this would not work like it is now, because the mixin would implement itself.``` --- I'm not sure I understand what do you mean in this sentence, or what is the problem itself, but by looking at your implementation something doesn't feel right. If you need 1 mixin for each sub-class, then the mixins are not that useful. If your goal is *only* to split a single class into multiple files, you can try something like [extensions](https://dart.dev/guides/language/extension-methods). If that's the case, I can elaborate and post an answer on that. – Naslausky Jan 26 '21 at 16:00
  • I already tried that, the Problem is, the method ´printAllPropeties()´ is an abstract method of the super type. Extensions can't override abstract methods. – puaaaal Jan 26 '21 at 17:07

0 Answers0