HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Listener pattern implementation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implementationpatternlistener

Problem

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);
  }
}


I have two questions:

  • Am I thinking correctly to remove onResponse code from every class and collect in one class.



  • How should I create an AsyncHelper class?

Solution

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.

The main idea is:

  • Create observable object.



  • Create as many listeners which implements common listener interface.



  • Register all created listeners for the observable object.



  • In the observable object call notifyAll code.



I also created a short article which explains the idea.

Context

StackExchange Code Review Q#6585, answer score: 4

Revisions (0)

No revisions yet.