mercredi 24 juin 2015

Avoid if else statement

I have 50 different classes. I made a generic class(All) with all these 50 classes.

public class All {

    private First first;
    private Second second;
    private Third third;
...


//GETTERS AND SETTERS
}

I have a generic method and inside this I have this piece of code:

 All all=new All();

       String result;
       if(all.getFirst()!=null){
           result=methodA(all.getFirst());
       }
       else if(all.getSecond()!=null){
           result=methodB(all.getSecond());
       }
       else if(all.getThird()!=null){
           result=methodC(all.getThird());
       }
...

I don´t like this configuration because it´s an unreadable code for so many classes.

How can I improve this code?

Aucun commentaire:

Enregistrer un commentaire