Object-oriented modeling is very subjective, but the only thing I can say here is the old discussion of Inheritance and composition :
https://www.thoughtworks.com/pt/insights/blog/composition-vs-inheritance-how-choose
, , , , . , , , is-a (.. - , - , - --). Java , , .
, , , -i-want-to-reuse, ().
, :
- . , , Mammals ( ). , hunt() Mammals.
: Mammals < - HuntingMammals. : , . , , , .
, . , , , Hunter Dog, Lion (). (), CanHunt .
:
class Hunter {
void hunt(){
System.out.println("i'm hunting...");
}
}
interface CanHunt{
Hunter getHunter();
}
class Dog extends Mammals implements CanHunt{
...
Hunter hunter = new Hunter();
@Override
Hunter getHunter(){
return hunter;
}
...
}
class Lion extends Mammals implements CanHunt{
...
Hunter hunter = new Hunter();
@Override
Hunter getHunter(){
return hunter;
}
...
}
, , :
...
List<CanHunt> hunters = new LinkedList();
hunters.add(new Dog());
hunters.add(new Lion());
for(CanHunt h:hunters){
h.getHunter().hunt();
}
...
, . , , . , , - , .