I have two classes Apple and Orange like this:
public final class Apple{
int getJuice();
}
public final class Orange{
int getJuice();
}
and I cannot change them. I have third class fruitsManeger like this:
class FruitManeger {
Apple apple;
Orange orange;
enum Fruits{
Apple,Orange
}
Fruits favorFruits;
int getJoice(){
if(favorFruits==Fruits.Apple){
apple.getJuice();
}else if(favorFruits==Fruits.Orange){
orange.getJuice();
}
}
}
My Question: what is best way to implement getJuice method in FruitManeger class?.
as you can see if I have a lot of fruits I should add a lot of if else expiration.
of course, I can use reflection to call methods by name but it's not a good idea when getJuice method return an object and you want to do something same with that.
is there any better way?