我的代码中有几个存储库类。
例如,这是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));
和UserRepository 中的CCD_ 2
不同于:
Call<ApiResponse> bonusCall = ApiService.getBonus(mPrefs.getString(User.TOKEN_NAME, null), mId);
和BonusRepository中的CCD_ 3。
在其他存储库中,我有类似的重复代码。
我想去掉这些重复的东西,但找不到任何好的解决办法。
干燥代码的最佳方法是什么?
为所有存储库创建一个抽象类父级,并实现getRemoteDataSource((方法,在每个特定实现中调用一个新的抽象方法,这将是唯一特定的方法。
例如:
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));
}
}
我让你适应你的需求,但这是最好的解决方案。