我该如何有效地使用事件总线? [英] How do I effectively use an event bus?

查看:151
本文介绍了我该如何有效地使用事件总线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在探索一些选项是机器人的学习项目。 我想用我的Rails API(也是一个学习的项目)进行通信。

Right now I am exploring some options for an android learning project. I am trying to communicate with my rails api (also a learning project).

之后做一些研究,我想我已经定居在使用改造和奥托的方案。

After doing some research, I think I have settled on a scheme that uses retrofit and otto.

我最终得到的是这一点。 当我想打个电话给我滑轨服务器(在这种情况下,做了注册)我这样做的活动。

What I end up with is this. When I want to make a call to my rails server (in this case to do a signup) I do this in the activity.

 mBus.post(new SignupRequestEvent(new UserRequestParams(mName,mEmail,mPassword,mPasswordConfirmation )));

,然后在相同的活动我都没有了。

and then in the same activity I have this.

@Subscribe
public void onSignupCompleted(SignupCompletedEvent event) {
    System.out.println(String.format("in onSignupCompleted, got token = %s ", event.getParams().getToken()));

}

这里的问题是,因为它的立场,每个API请求类型,它对应的响应类型将是一个独特的事件类型,并要求它自己的类,它好像很多锅炉板code型。

The problem here is that, as it stands, every api request type and it corresponding response type would be a unique event type and require it's own class, which seems like a lot of boiler plate type of code.

例如,以处理登录和注销我需要这两个类:

For example to handle sign in and sign out I would need these two classes:

public class SignupRequestEvent {
    protected UserRequestParams mSignupParams;

    public SignupRequestEvent(UserRequestParams signupParams) {
        mSignupParams = signupParams;
    }

    public UserRequestParams getParams() {
        return mSignupParams;
    }

}

public class SignupCompletedEvent {

    private SignupCompletedParams mSignupCompletedParams;
    public SignupCompletedParams getParams() {
        return mSignupCompletedParams;
    }
    public SignupCompletedEvent(SignupCompletedParams signupCompletedParams) {
        mSignupCompletedParams = signupCompletedParams;
    }

}

和我想大多数的事件类将是pretty的很多是相同的。

And I think most of the event classes would be pretty much identical.

我想我应该有2个事件API调用,一个用于请求,一个用于响应,但每个接收API响应事件需要检查它是否为所需的请求的响应方式。

I am thinking I should just have 2 events for api calls , one for requests and one for responses, but then each method that receives an api response event would need to check if it is a response to the desired request.

这个选项将意味着是这样的:

This option would mean something like this:

ApiRequestEvent apiRequestEvent = new ApiRequestEvent();
apiRequestEvent.setAction("SIGNUP");
apiRequestEvent.setParameters(new UserRequestParams(mName,mEmail,mPassword,mPasswordConfirmation ));
mBus.post(apiRequestEvent);

和再处理响应是这样的:

and then to handle the response something like this:

@Subscribe
public void onSignupCompleted(ApiResponseAvailable event) {
    if (event.getResponseTo != "SIGNUP") return;
    System.out.println(String.format("in onSignupCompleted, got token = %s ", event.getParams().getToken()));

也许有一种方法可以使用​​泛型?

Maybe there is a way to use generics?

有人可以解释如何有效地使用事件总线,当有一组事件,可以组合在一起也是这样吗?

Can someone explain how to effectively use an event bus when there are a set of events that can be grouped together like this?

推荐答案

你这得太多 - 只是继续前进,为每一个事件的消息对象

You're overthinking it - just go ahead and create a message object for each event.

这篇关于我该如何有效地使用事件总线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆