Jesus Dimrix Jesus Dimrix -4 years ago 85
Android Question

Best practice in java to create my own interface instead of existing one in retrofit for example

I am building the network structure in my android application i one of the things i realized is that i want to do is to create my own interface in my requests class .

i have this method for example :

public static void getUserData(String owner, final DataManager.OnDataReceived<Owner> listener) {
Call<Owner> call = getGitService().getUser(owner);
call.enqueue(new Callback<Owner>() {
@Override
public void onResponse(Call<Owner> call, Response<Owner> response) {

}

@Override
public void onFailure(Call<Owner> call, Throwable t) {

}
});
}


now...all my methods respond same way to the call back so why not handle this one time ?

so the way i did it it to create class that implement retrofit Callback interface :

private static class callbackHandler implements Callback {

final DataManager.OnDataReceived listener;

callbackHandler(DataManager.OnDataReceived listener) {
this.listener = listener;
}

@Override
public void onResponse(Call call, Response response) {
listener.onDataReceived(response.body(), getErrorFromResponse(response));
}

@Override
public void onFailure(Call call, Throwable t) {
listener.onDataReceived(null, t.toString());
}
}


so now all the request look like this :

public static void getUserData(String owner, final DataManager.OnDataReceived<Owner> listener) {
Call<Owner> call = getGitService().getUser(owner);
call.enqueue(new callbackHandler(listener));
}


much clearer ...

1 . what do you think about this solution ? i had better way to handle all Callback same way ?
2 . the compiler shout at me that

call.enqueue(new callbackHandler(listener));


and

@Override
public void onResponse(Call call, Response response) {
listener.onDataReceived(response.body(), getErrorFromResponse(response));
}

@Override
public void onFailure(Call call, Throwable t) {
listener.onDataReceived(null, t.toString());
}


and my interface :

public interface OnDataReceived<T> {
void onDataReceived(T data, String error);
}


is unchecked assignment...i understand what that means but not sure how to fix this ?

Answer Source

what do you think about this solution ?

This is called the adapter pattern. You are adapting one interface (Callback<Owner>) to another (DataManager.OnDataReceived<Owner>).

If your objective is to be able to replace Retrofit with something else in the future, this sort of adapter is perfectly reasonable. Otherwise, you might consider just having DataManager.OnDataReceived extend Callback and change your method names to match (e.g., onDataReceived() turns into onResponse()), to avoid the need for this adapter.

i understand what that means but not sure how to fix this

callbackHandler implements Callback wipes out the Java generics.

If this is only for use with Owner, use callbackHandler implements Callback<Owner> and have it hold a DataManager.OnDataReceived<Owner>. If you plan to use this for multiple model objects (assuming that Owner is a model), use callbackHandler implements Callback<T> and have it hold a DataManager.OnDataReceived<T>.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download