如何在 handlerManager 中捆绑处理程序?

发布于 2024-12-18 15:33:55 字数 278 浏览 0 评论 0原文

是否有一种选项可以将不同的事件处理程序捆绑在一个java文件中?

就像:

public interface MyHandlerr extends EventHandler {
void myEvent1(Event1 event);
void myEvent2(Event2 event);
}

目前我为每个事件都有一个处理程序......但我对此不满意。

格雷茨

is there a option to bundle different eventhandler in one javafile?

Like:

public interface MyHandlerr extends EventHandler {
void myEvent1(Event1 event);
void myEvent2(Event2 event);
}

in the moment i have for each event one handler....but i'm not happy with it.

greetz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汹涌人海 2024-12-25 15:33:55

您可以创建自己的 EventHandler 接口来处理多个事件

public interface MultipleEventsHandler extends EventHandler {
    void onMyEvent(MyEvent event);
    void onMyOtherEvent(MyOtherEvent event);
}

然后在事件类中您可以定义应该调用哪些方法

public class MyEvent extends GwtEvent<MultipleEventsHandler> {
    public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();

    @Override
    public Type<MultipleEventsHandler> getAssociatedType() {
            return TYPE;
    }

    @Override
    protected void dispatch(MultipleEventsHandler handler) {
            handler.onMyEvent(this);
    }
}

public class MyOtherEvent extends GwtEvent<MultipleEventsHandler> {
    public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();

    @Override
    public Type<MultipleEventsHandler> getAssociatedType() {
            return TYPE;
    }

    @Override
    protected void dispatch(MultipleEventsHandler handler) {
            handler.onMyOtherEvent(this);
    }
}

如果您只是想减少类/接口的数量,那么您可以放置​​ EventHandler 在你的事件类中,例如

public class MyEvent extends GwtEvent<MyEvent.Handler> {

    public interface Handler extends EventHandler {
        void onMyEvent(SomeEvent event);
    }

    public static final Type<MyEvent.Handler> TYPE = new Type<MyEvent.Handler>();


    @Override
    public Type<MyEvent.Handler> getAssociatedType() {
            return TYPE;
    }

    @Override
    protected void dispatch(MyEvent.Handler handler) {
            handler.onMyOtherEvent(this);
    }
}

You can create your own EventHandler interface for handling multiple events

public interface MultipleEventsHandler extends EventHandler {
    void onMyEvent(MyEvent event);
    void onMyOtherEvent(MyOtherEvent event);
}

Then in your event classes you can define which of the methods should be called

public class MyEvent extends GwtEvent<MultipleEventsHandler> {
    public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();

    @Override
    public Type<MultipleEventsHandler> getAssociatedType() {
            return TYPE;
    }

    @Override
    protected void dispatch(MultipleEventsHandler handler) {
            handler.onMyEvent(this);
    }
}

public class MyOtherEvent extends GwtEvent<MultipleEventsHandler> {
    public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();

    @Override
    public Type<MultipleEventsHandler> getAssociatedType() {
            return TYPE;
    }

    @Override
    protected void dispatch(MultipleEventsHandler handler) {
            handler.onMyOtherEvent(this);
    }
}

If you just want to reduce number of classes/interfaces then you can put EventHandler's inside your event classes, e.g.

public class MyEvent extends GwtEvent<MyEvent.Handler> {

    public interface Handler extends EventHandler {
        void onMyEvent(SomeEvent event);
    }

    public static final Type<MyEvent.Handler> TYPE = new Type<MyEvent.Handler>();


    @Override
    public Type<MyEvent.Handler> getAssociatedType() {
            return TYPE;
    }

    @Override
    protected void dispatch(MyEvent.Handler handler) {
            handler.onMyOtherEvent(this);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文