我有一个AsyncTask
我需要将多个参数传递给我的构造函数。我知道它的错误做法是要传递太多的参数,并且更好地将方法分解成更小的块来避免这种情况,但是我不允许打破这个方法,所以我唯一的方法是找到一个更好的方法来传递参数。在我的AsyncTask
中,我创建了一个构造函数。如何避免在AsyncTask中传递太多输入参数?
是否可以使用value对象创建模型类并传递它们?我需要吸气剂和吸附剂吗?
class UpdateAsyncTask extends AsyncTask<Void, Void, Void> {
private String productId;
int mutationAmount;
boolean isOpeningStock;
FlightLegIdentifier fli;
String crew;
String tabletId;
UpdateAsyncTask(final String productId, final int mutationAmount, final boolean isOpeningStock,
final String flightKey,
final FlightLegIdentifier fli,
final String crew,
final String tabletId,
final AirFiApplication airFiApplication) {
this.productId = productId;
this.mutationAmount = mutationAmount;
this.isOpeningStock = isOpeningStock;
this.fli = fli;
this.tabletId = tabletId;
this.crew = crew;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
productStockAdapter.notifyDataSetChanged();
}
@Override
protected Void doInBackground(Void... params) {
StockUtils.saveMutation(productId, mutationAmount, isOpeningStock, flightKey, fli, crew,
tabletId, getAirFiApplication());
return null;
}
}
您可以改为创建一个POJO,并把它传递给Aysnctask –
您可以创建一个模型类,然后传递模型分成UpdateAsyncTask构造。同时在你的模型中创建setter和getters –
http://www.javaworld.com/article/2074938/core-java/too-many-parameters-in-java-methods-part-3-builder-pattern.html – ajb