Support for Request - Response style interaction

5 views
Skip to first unread message

Dennis

unread,
Dec 10, 2010, 1:43:25 PM12/10/10
to Mvp4g
Hi,

In my app I used the 'request-response' style communication pattern
several times already. I was wondering if this would be interesting to
add to the library (like the recently introduced broadcast feature).
Currently I am doing something like:
1. Requesting presenter creates request event, like
requestPrice(String id) and pass in the id of the presenter that
sends the request (id is manually created)
2. Receiving presenter creates response event, like requestPriceReply
(String senderId) and passes in the id it received
3. Requesting presenter gets reply and checks if the id is his id, if
so then process reply otherwise skip it

Did you already encounter a similar situation? Do you think it would
be worthy to have buil-tin support for this communication pattern in
the library?

cheers,
Denis

Pierre

unread,
Dec 11, 2010, 11:30:52 AM12/11/10
to Mvp4g
This situation is bit similar to the reason why I implemented the
activate/deactivate feature (http://groups.google.com/group/mvp4g/
browse_thread/thread/76e36c0cbc59d3ec/c2211f3944c9f6a9). Basically I
wanted only one presenter to receive the response so I would
deactivate the other ones. In my case, only one presenter was
displayed at a time so when the presenter's view was hidden, I
deactivated the presenter.

The activate/deactivate feature may not work as well if you have
several presenters displayed. Also a drawback with this feature is
that the configuration can become complicated.

For this kind of issue, I now tend to use the callback solution
suggested by Mohit in the other post.

You can then have a callback event class like this:

public interface EventCallback<T> {

void reply(T replyInfo);

}

and with your event, you pass a callback object:

void requestPrice(EventCallback<Integer> callback);

then the presenter that handles it use it to call back the sender:

public void onRequestPrice(EventCallback<Integer> callback){

int price = ....
callback.reply(price);

}

What do you think of this solution?

Pierre
Reply all
Reply to author
Forward
0 new messages