I have created the following class:
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...
}
}
}
This class is not finished yet and I still have to add here another 10 other network calls I perform all around the application.
To run one of the network tasks I run this line for example:
AsyncHttpsClientHelper.getInstance(this).performNetworkTask(AsyncHttpsClientHelper.COMMIT_COUPON_TO_SERVER, event.getValue());
When the task finishes I fire an Event
using the Square
event Bus
tool to commit the needed visual changes to the activity/ fragment layout.
The Question: My boss claims that this is a bad practice and that this class will become a mess when I finish it. More over he claims that this class should be a stupid class that all he knows how to do is to configure the AsyncHttpClient
object and return it so I could use it and perform the https tasks inside the relevant Activity
. Basically he says the the https calls themselves should be located in the Activity classes. I like this way better and think it makes my activities more cleaner and nicer to read. He on the other hand says that this way its harder to debug and that this class combines a part of Controller and View functionality which it should not do.
So who is right? is it really a bad practice to create a class like this?
Helper
you have an ill defined object it is known as a code smell – BoudoirWeakReferences
of perishable goods such asActivity
,Context
etc.? – Silin