3

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?

5
  • i cannot change apple and orange class Commented Nov 30, 2016 at 5:57
  • We will just have to ask why you cannot change Apple and Orange... because the huge amount of if/case statements you'd need is a direct result of that. By the way you can use Reflection to handle the returned object as well.... you'd just need more if/case statements. Commented Nov 30, 2016 at 5:59
  • because Apple and orange is in java library Commented Nov 30, 2016 at 6:00
  • 1
    Apple and Orange are non final classes, extend those then... look my suggested answer please... Commented Nov 30, 2016 at 6:02
  • 1
    I think you should read about factory design pattern.It is perfect scenario for your question. Commented Nov 30, 2016 at 6:06

3 Answers 3

15

is there any better way?

Yes, Refactor that application into a more oop way...

Remember: you cannot Modify Apple or Orange, but OOP allows you extending those classes for sure since they are not final...

class CorrectApple extends Apple implement IJuiceble{
    @Override
    int getJuice();
}
class CorrectOrange extends Orange  implement IJuiceble{
    @Override
    int getJuice();
}

now the

interface IJuiceble{
    int getJuice();
}

and finally the manager:

class FruitManeger {
     private CorrectApple apple;
     private CorrectOrange orange;

     int getJoice(IJuiceble correctfruit){
         return correctfruit.getJuice();
     }
}
Sign up to request clarification or add additional context in comments.

5 Comments

a moving target
@max well if you dont provide the correct/complete information then this answer is not even going to compile... and my design is now useless....
You could still make a CorrectApple that has an Apple.
Can we achieve same thing using annotations? That would save redefining all classes?
Question: Should FruitManager also implement IJuicable as a good practice? Should the class FruitManager be called FruitFactory as a good practice? Should we have a List storing all the fruits in FruitManager so we can easily loop through the fruits?
0

You can use switch instead of if/else.

class FruitManager {
    Apple apple;
    Orange orange;

    enum Fruits {
        Apple, Orange
    }

    Fruits favorFruits;

    int getJoice() {
        switch (favorFruits) {
        case Apple:
            apple.getJuice();
            break;
        case Orange:
            orange.getJuice();
            break;

        default:
            System.err.println("Fruit not found. " + favorFruits);
        }
    }

3 Comments

the more fruits you have in the manager the bigger the switch case and the enum....
It's a bad solution, but is a solution for the question. I prefer the interface solution as well.
I like the way you approached anyway\
-1

By referring this Answer

interface IScalable {
   void setScale(int scale);
   int getScale();
}

class DataSizeAction extends EncoderAction implements IScalable {
   ...
}

class SomeoneElse {
   private int scale = 2;

   public void setScale(IScalable scalable) {
      scalable.setScale(this.scale);
   }
}

1 Comment

The question stated, for whatever reason, the classes Apple and Orange cannot be changed, and they don't implement or extend anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.