Is there a way to reuse builder code for retrofit

2020-02-07 17:50发布

问题:

I am using Retrofit and in every task I have to do something like this:

public class MyTask extends AsyncTask<String, String, String> {

    private void someMethod() {
        final RestAdapter restAdapter = new RestAdapter.Builder()
            .setServer("http://10.0.2.2:8080")
            .build();
        final MyTaskService apiManager = restAdapter.create(MyTaskService.class);
    }

    // ...

}

What is a good way to make this code DRY?

回答1:

first you declare your parent class with all common behavior

public abstract class MyAbstractTask extends AsyncTask<String, String, String> {

 protected void someMethod() { //note that i change private to protected
  final RestAdapter restAdapter = new RestAdapter.Builder().setServer("http://10.0.2.2:8080").build();
  final MyTaskService apiManager = restAdapter.create(MyTaskService.class);
 }

}

then, you extend it with every task

public   class MyTask extends MyAbstractTask {

 //your someMethod() is available from everywhere in your class

}

public  class MyOtherTask extends MyAbstractTask {

 //your someMethod() is available from everywhere in your class

}

but i dont know where you are using restAdapter and apiManager, and if the actually need to be created once per task, since probably you can create it outside of these tasks.

If you create them outside, and then you need to use something inside your task, it is also good to have in mind the Dependency_injection pattern.


Also, you should avoid hardcoding values in your classes, like http://10.0.2.2:8080

You should use at least a final static final String server= "http://10.0.2.2:8080" and then use that, or better, use a setter or the constructor in the most inner class, and set tha values from the activity or the main controller.



回答2:

Both the RestAdapter and the generated instance of your services (MyTaskService in this case) are extremely expensive objects and should be used as singletons.

This means that you should only ever call restAdapter.create once and re-use the same instance of MyTaskService every time you need to interact with.

I cannot stress this enough.

You can use the regular singleton pattern in order to ensure that there only is ever a single instance of these objects that you use everywhere. A dependency injection framework would also be something that could be used to manage these instances but would be a bit overkill if you are not already utilizing it.



回答3:

As Jake said, you should use the singleton pattern in order to ensure the same instance is always used.

Here's an example:

public class ApiManager {

    public interface GitHubService {

        @GET("/users/{user}/repos")
        List<Repo> listRepos(@Path("user") String user);

    }

    private static final String API_URL = "https://api.github.com";

    private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder()
        .setEndpoint(API_URL)
        .setLogLevel(LogLevel.FULL)
        .build();

    private static final GitHubService GIT_HUB_SERVICE = REST_ADAPTER.create(GitHubService.class);

    public static GitHubService getService() {
        return GIT_HUB_SERVICE;
    }
}

You can use the service in this example like so:

ApiManager.getService().listRepos(...);