我创建了以下类:
public class AsyncHttpsClientHelper {
public static final int REMOVE_CREDIT_CARD = 1;
public static final int ENABLE_AUTORENEW = 2;
// +10 final ints...
private static AsyncHttpsClientHelper instance = null;
private static Activity activity;
// Some other variables
private AsyncHttpsClientHelper(Context context) {
// Initiate variables
}
public static AsyncHttpsClientHelper getInstance(Context context) {
// Guarantees the same instance for this class (Singleton)
}
public void performNetworkTask(int networkTaskType, String value)
{
switch (networkTaskType)
{
case REMOVE_CREDIT_CARD:
{
CupsLog.d(TAG, "path: " + Consts.ACCOUNT_REMOVE_CREDIT_CARD_PATH);
client.post(Consts.ACCOUNT_REMOVE_CREDIT_CARD_PATH , new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject result) {
try {
CupsLog.d(TAG, Consts.ACCOUNT_REMOVE_CREDIT_CARD_PATH + " -> onSuccess, result: " + result.toString(3));
AccountService.getInstance(activity).pullAccountDetailsFromServer();
Toast.makeText(activity, "Your credit card was removed", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable arg0) {
CupsLog.d(TAG, "commitPaymentToServer -> onFailure");
BusProvider.getInstance().post(new DialogChangeEvent(DialogChangeEvent.REMOVE_CREDIT_CARD, "failed"));
}
});
break;
}
case ENABLE_AUTORENEW:
{
// Do ENABLE_AUTORENEW logic
}
// +10 cases goes here...
}
}
}
这门课还没有完成,我还需要在这里添加另外 10 个我在应用程序周围执行的其他网络调用。
要运行其中一个网络任务,我运行这一行,例如:
AsyncHttpsClientHelper.getInstance(this).performNetworkTask(AsyncHttpsClientHelper.COMMIT_COUPON_TO_SERVER, event.getValue());
Event
当任务完成时,我使用Square
事件工具触发Bus
向活动/片段布局提交所需的视觉更改。
问题:我的老板声称这是一种不好的做法,当我完成这门课时会变得一团糟。此外,他声称这个类应该是一个愚蠢的类,他所知道的只是配置AsyncHttpClient
对象并返回它,这样我就可以使用它并在相关的Activity
. 基本上他说 https 调用本身应该位于 Activity 类中。我更喜欢这种方式,并认为它使我的活动更清晰,更易于阅读。另一方面,他说这种方式更难调试,并且这个类结合了控制器和视图功能的一部分,这是它不应该做的。
那么谁是对的呢?创建这样的课程真的是一种不好的做法吗?