java – Is there a way to reuse builder code for retrofit-ThrowExceptions

Exception or error:

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()
        final MyTaskService apiManager = restAdapter.create(MyTaskService.class);

    // ...


What is a good way to make this code DRY?

How to solve:

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("").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

You should use at least a final static final String server= "" 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.


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.


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 {

        List<Repo> listRepos(@Path("user") String user);


    private static final String API_URL = "";

    private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder()

    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:


Leave a Reply

Your email address will not be published. Required fields are marked *