I am using Retrofit to interact with my REST API, and was wondering whether anybody has any design suggestions.
My app has the following packages:
- models
- services
- activities
- fragments
The services package contains the interfaces for Retrofit. For example:
public interface FooService {
@FormUrlEncoded
@POST("foo/do")
@Headers("Content-Type: application/x-www-form-urlencoded; charset=UTF-8")
Call<FooBar> do();
}
Models contains...well, the different models. For example, FooBar. So far so good - just as per Retrofit documentation.
I have created an API class, that handles the Retrofit build logic (Retrofit retrofit = new Retrofit.Builder()
etc), and exposes a static field: retrofit.In my activities I then can perform my requests as follows:
FooService service = API.retrofit.create(FooService.class);
Call<FooBar> call = service.do();
try {
retrofit2.Response response = call.execute();
// ...do stuff...
} catch(IOException) {}
And herewith comes my question: Would it be better to abstract the above further? So that I would not need to repeat the above everywhere? For example, something like:
MyOtherFooService service = new MyOtherFooService();
FooBar fooBar = service.do();
Any thoughts? Recommendations?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…