我的代码中有几个存储库类。
例如,这是UserRepository:
public class UserRepository {
public static String TAG = "UserRepository";
ApiService mApiService;
SharedPreferences mPrefs;
Context mContext;
RemoteDataSource<User> mRemoteDataSource;
public UserRepository() {
mApiService = new RetrofitClient().getApiService();
mContext = App.getAppContext();
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
mRemoteDataSource = new RemoteDataSource<>();
}
public RemoteDataSource getRemoteDataSource() {
mRemoteDataSource.setIsLoading();
Call<ApiResponse> userCall = mApiService.getUserInfo(mPrefs.getString(User.TOKEN_NAME, null));
userCall.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
mRemoteDataSource.setIsLoaded();
mRemoteDataSource.setData(response.body().getUser());
mRemoteDataSource.setStatus(response.body().getStatus());
mRemoteDataSource.setMessage(response.body().getMessage());
}
@Override
public void onFailure(Call<ApiResponse> call, Throwable t) {
Log.e(TAG, t.getMessage());
mRemoteDataSource.setFailed(t.getMessage());
}
});
return mRemoteDataSource;
}
}
这是BonusRepository:
public class BonusRepository {
public static String TAG = "BonusRepository";
ApiService mApiService;
SharedPreferences mPrefs;
Context mContext;
LiveData<Bonus> mBonus;
String mId;
RemoteDataSource<Bonus> mRemoteDataSource;
public BonusRepository(String id) {
mId = id;
mApiService = new RetrofitClient().getApiService();
mContext = App.getAppContext();
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
mRemoteDataSource = new RemoteDataSource<>();
}
public RemoteDataSource getRemoteDataSource() {
mRemoteDataSource.setIsLoading();
Call<ApiResponse> bonusCall = mApiService.getBonus(mPrefs.getString(User.TOKEN_NAME, null), mId);
bonusCall.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
mRemoteDataSource.setIsLoaded();
mRemoteDataSource.setData(response.body().getBonus());
mRemoteDataSource.setStatus(response.body().getStatus());
mRemoteDataSource.setMessage(response.body().getMessage());
}
@Override
public void onFailure(Call<ApiResponse> call, Throwable t) {
Log.e(TAG, t.getMessage());
mRemoteDataSource.setFailed(t.getMessage());
}
});
return mRemoteDataSource;
}
}
除这两个类中的 getRemoteDataSource
方法均相等之外,
Call<ApiResponse> userCall = mApiService.getUserInfo(mPrefs.getString(User.TOKEN_NAME, null));
和mRemoteDataSource.setData(response.body().getUser());
在用户存储库中
与以下不同:
Call<ApiResponse> bonusCall = ApiService.getBonus(mPrefs.getString(User.TOKEN_NAME, null), mId);
和mRemoteDataSource.setData(response.body().getBonus());
在BonusRepository中。
在其他存储库中,我有类似的重复代码。
我想删除这些重复项,但找不到任何好的解决方案。
干燥我的代码的最佳方法是什么?
答案 0 :(得分:1)
为所有存储库的父类创建一个抽象类父类,并实现 getRemoteDataSource()方法,并在每个特定实现中调用一个新的抽象方法,该方法将是唯一的特定抽象方法。 / p>
例如:
public class AbstractRepository {
protected abstract Call<ApiResponse> performCall();
public RemoteDataSource getRemoteDataSource() {
mRemoteDataSource.setIsLoading();
Call<ApiResponse> userCall = performCall();
userCall.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
mRemoteDataSource.setIsLoaded();
mRemoteDataSource.setData(response.body().getUser());
mRemoteDataSource.setStatus(response.body().getStatus());
mRemoteDataSource.setMessage(response.body().getMessage());
}
@Override
public void onFailure(Call<ApiResponse> call, Throwable t) {
Log.e(TAG, t.getMessage());
mRemoteDataSource.setFailed(t.getMessage());
}
});
return mRemoteDataSource;
}
}
然后您可以执行以下操作:
public class UserRepository extends AbstractRepository {
public static String TAG = "UserRepository";
ApiService mApiService;
SharedPreferences mPrefs;
Context mContext;
RemoteDataSource<User> mRemoteDataSource;
public UserRepository() {
mApiService = new RetrofitClient().getApiService();
mContext = App.getAppContext();
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
mRemoteDataSource = new RemoteDataSource<>();
}
protected Call<ApiResponse> performCall() {
return mApiService.getUserInfo(mPrefs.getString(User.TOKEN_NAME, null));
}
}
我让您适应您的需求,但这是最好的解决方案。