In my project, I am doing asynchronous processes in almost every classes. To explain my problem, I have created a sample
public interface TestListener {
void onResponse(Response result);
void onError(Exception e);
}
public class AsyncTask {
TestListener testListener;
public AsyncTask(TestListener testListener, Request request)
{
}
public void excute()
{
try {
// process request
Response response = new Response();
testListener.onResponse(response);
} catch (Exception e) {
testListener.onError(e);
}
}
}
public class Test1 implements TestListener {
public void doStuff()
{
Request request = new Request();
// add some stuff to request;
AsyncTask task = new AsyncTask(this, request);
task.excute();
}
@Override
public void onResponse(Response result)
{
// process response
}
@Override
public void onError(Exception e)
{
// process error
}
}
public class Test2 implements TestListener {
void someStuff()
{
Request request = new Request();
// add some stuff to request;
AsyncTask task = new AsyncTask(this, request);
task.excute();
}
@Override
public void onResponse(Response result)
{
// process response
}
@Override
public void onError(Exception e)
{
// process error
}
}
This doesn't look good to me. In every class I have onResponse and onError. What I want to do is just create a helper class that will have onResponse and onError and it will return me the response.
public class Test1{
public void doStuff(){
AsyncHelper helper = AsyncHelper.getInstance();
Response response = helper.test1Method(request);
}
}
So I have two questions.
- Am I thinking correctly to remove
onResponsecode from every class and collect in one class. - How should I create an
AsyncHelperclass?
