开发者

Is passing interface to AsyncTask a good practice

开发者 https://www.devze.com 2023-04-01 02:48 出处:网络
I have an interface class public interface AsyncTaskExecuteCommand { public Object executeCommand(String jsonLocation) throws IOException,JSONException;

I have an interface class

public interface AsyncTaskExecuteCommand {
public Object executeCommand(String jsonLocation) throws IOException,JSONException;
}

And I have a HashMap which store instances of this interface

public static HashMap<String,AsyncTaskExecuteCommand> executeCommandHashMap

executeCommandHashMap.put(COMMAND_CORE_FIELD_FETCH, new AsyncTaskExecuteCommand() {
        @Override
        public Object executeCommand(String jsonLocation) throws IOException, JSONException{
                           //return some thing
        }
    });

executeCommandHashMap.put(COMMAND_REGISTER_FIELD_FETCH, new AsyncTaskExecuteCommand() {
        @Override
        public Object executeCommand(String jsonLocation) throws IOException,
                JSONException {
                          //re开发者_StackOverflowturn some thing
        }
    });

And my AsyncTask named GeneralAsyncTask includes

doInBackground(){

 AsyncTaskExecuteCommand asyncTaskExecuteCommand = executeCommandHashMap.get(params[0]);
 return asyncTaskExecuteCommand.executeCommand(params[1]);
}

And this AsyncTask is called

new GeneralAsyncTask().execute(COMMAND_REGISTER_FIELD_FETCH,"http://something");

I have done this because the general structure of my AsyncTask remains the same i.e.,it does some method execution and return some value.Only method execution type and return value will be different.If I won't implement passing interface to async task,I end up creating lots of AsyncTask classes. So,is this method a good to way to tackle my scenario?


Seems like a lot of complexity. Is there a reason you just don't use an anonymous class:

new AsyncTask<String, Void, Object>() {
    @Override
    protected Object doInBackground(String... url) {
        //return some thing
    }
    protected void onPostExecute(Object result) {
        // do something with result
    }
}.execute("http://something");


The interface makes sense if you are going to code the implementations and have them as separate classes in some package. This improves readability. If the purpose is not writing the AsynkTask each time, because it has some common parts but only the doInBackground changes, then I'd extend the AsyncTask to a concrete generic class accepting a GENERIC interface like this one:

    public interface MyTask<T,R> {
        R doInBackground(T... param);
    }

The class would be something like this (not tested):

    public class MyAsyncTask<T, P, R> extends AsyncTask<T, P, R> {
        private MyTask<T,R> task;

        public MyAsyncTask(MyTask<T,R> todo){
            task = todo;
        }

        protected R doInBackground(T... params) {
            if(task != null){
                return task.doInBackground(params);
            } else {
                return null;
            }
        }

        //Other AsyncTask mandatory methods implemented here. 

    }

Or if you are fine without generics, then do the interface and the AsyncTask subclass non generic. Either way, I'd code the task(s) class(es) and command implementations, put them on a package, make them singletons (or have public references in some main class) and get rid of the map.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号