Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

  1. Am I thinking correctly to remove onResponse code from every class and collect in one class.
  2. How should I create an AsyncHelper class?
share|improve this question
    
Is the functionality of Test1.onResponse the same as Test2.onResponse? –  Bobby Dec 7 '11 at 7:55
    
nope they are different.e.g test1 is login so I will get authentication response and test2 is profile so I will get user details in test2 response. –  Vivart Dec 7 '11 at 10:31
    
Then how would you want to extract them into a helper method? I mean, without cluttering up that Class? –  Bobby Dec 7 '11 at 10:36
    
So you'd like to call your tasks synchronously? What should happen in case of error? –  palacsint Dec 8 '11 at 12:03
    
@palacsint In case of success it will show progress bar and return return Response object. In case of error it will show error dialog and it will return null. I am starting this project so i want to make sure that which way to go. because after some time it hard switch the way. –  Vivart Dec 8 '11 at 12:22

2 Answers 2

Naming the classes and the design (at least as far as I can see from the code you pasted) suggests that you want to use the observer pattern. The main idea of this pattern is to use listeners which implement common listener interface and register them for the specific object which you want to make 'observable'. Then in the observable object you can call notifyAllListeners() method or notifyAllListenersOnError() or some similar which will do nothing else than

for listener from registered listeners call listener.notify() (or listener.onError() etc.).

Methods notify(), onError() etc have to be declared in the common listener interface.

What I mean is described here http://en.wikipedia.org/wiki/Observer_pattern.

So to resume the main idea is.

  1. Create observable object.

  2. Create as many listeners which implements common listener interface.

  3. Register all created listeners for the observable object.

  4. In the observable object call notifyAll code.

enter image description here

I created also a short article which explains the idea.

http://codeblocker.blogspot.co.at/2012/11/human-nature-patterns-observer-pattern.html

Lukasz

share|improve this answer
    
notify is probably a bad idea in Java. –  codesparkle Nov 1 '12 at 12:31
    
above it is just a pseudo code. and the explanation of the observer pattern which can be implemented in many languages. –  Łukasz Rzeszotarski Nov 1 '12 at 12:34

Check out the Observable class and Observer interface in java.util. You can extend Observable without overriding any of its methods for most simple implementations.

java.util.Observable

java.util.Observer

.NET's version of these interfaces are a little different, but might provide some different ideas that could be implemented in Java pretty easily.

MSDN: IObservable(T) Interface

MSDN: IObserver(T) Interface

The mechanics of how each operate are slightly different:

  • In Java, a class extending Observable marks itself as changed and calls a single method to notify all Observers when it's ready.

  • In .NET, a class implementing IObservable<T> maintains its own list of IObserver<T>s, and iterates over this list when ready, calling the appropriate notification (Error, Next, or Finished). This is likely closer to what you want and could be implemented in Java rather easily.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.