my AsyncTask doInBackground have multiple nested condition which return String to onPostExectue method , which make code very complex is there any alternative way to handle this problem
Now my code look like this
protected String doInBackground(Void... params){
if (position == 0){
return "Set1"
}
else if(Position >something) {
return set 2
} .........and so on
}
@Override
protected void onPostExecute(String result) {
if (result.equals("set1")){
// do some taskk
}
elseif(result.equal("Set2")){
// do other task
}
else if(){
// sooo onnnnnn
}
}
thanks in advance for your help
One can try to apply Strategy pattern here. You can create an abstract class, say
Action, with an abstract functionperform(). Then you can create concrete subclasses ofActionfor every action which can be performed in onPostExecute. So you have classes sayActionForSet1,ActinForSet2etc with concrete implementations of perform(). ThenActionshould have a static methodString Action createfromString(String set)which will be essentially the same if/else or break but much more maintainable. It will return corresponding Action subclasses for every string. You can use Map instead of if/else in there, easier to add/remove elements then. Then after you have yourActionobject returned byAction.createFromStringyou call itsperform()