Java 中更轻松的自定义事件处理

发布于 2024-11-10 08:56:43 字数 1167 浏览 0 评论 0原文

在Java中,每次我想创建一个新的自定义事件时,我通常通过添加3个方法来完成,即:

addDogEventListener(EventListener listener); 
removeDogEventListener(EventListener listener);
dispatchDogEventListener(DogEvent event);

然后现在如果我想调度另一个事件,比如CatEvent,我将不得不再次创建所有这3个方法:

addCatEventListener(EventListener listener); 
removeCatEventListener(EventListener listener);
dispatchCatEventListener(CatEvent event);

然后如果我只想管理一种 CatEvent 事件,说喵,我必须再次复制并粘贴所有这 3 个方法?!像 addCatMeowEventListener();... 等?

通常,我需要调度不止一种事件。让整个类充满这么多的方法来传输和处理事件,会很不整洁。不仅如此,这些函数具有非常相似的代码,例如循环遍历 EventListenerList、将事件添加到列表等。

这是我应该如何在 Java 中进行事件调度吗?

有没有一种方法可以像我一样:

mainApp.addEventListener(CatEvent.MEOW, new EventHandler() { meowHandler(Event e) { });
mainApp.addEventListener(CatEvent.EAT, new EventHandler() { eatHandler(Event e) { });
myCat.addEventListener(DogEvent.BARK, new EventHandler() { barkHandler(Event e) { myCat.run() });

通过这种方式,我可以在不同的 eventHandler 类和函数中处理不同类型的 CatEvent,而不必继续为不同的事件创建不同的事件侦听器方法?

也许我遗漏了一些关于 Java 事件处理的内容,但是有没有一种更简洁的方法,让我不必不断复制和粘贴这 3 个方法,并为我想要分派的每种不同类型的方法创建如此多不同类型的事件对象?

谢谢!

In Java, everytime I want to create a new custom event, I usually do it by add 3 methods namely:

addDogEventListener(EventListener listener); 
removeDogEventListener(EventListener listener);
dispatchDogEventListener(DogEvent event);

Then now if I want to dispatch another event, say CatEvent, I will have to create all these 3 methods again:

addCatEventListener(EventListener listener); 
removeCatEventListener(EventListener listener);
dispatchCatEventListener(CatEvent event);

Then if I want to manage just one kind of CatEvent event, say Meow, I have to copy and paste all these 3 methods again?! Like addCatMeowEventListener();... etc?

And usually, I need to dispatch more than one kind of events. It will be very untidy to have the whole class filled with so many methods to transmit and handle the events. Not only that, these functions have very similar code, like loop through the EventListenerList, add event to the list, etc.

Is this how I should do event dispatching in Java?

Is there a way like I can do it like:

mainApp.addEventListener(CatEvent.MEOW, new EventHandler() { meowHandler(Event e) { });
mainApp.addEventListener(CatEvent.EAT, new EventHandler() { eatHandler(Event e) { });
myCat.addEventListener(DogEvent.BARK, new EventHandler() { barkHandler(Event e) { myCat.run() });

In this way, I can just handle the different types of CatEvent in different eventHandler class and functions and I don't have to keep creating different event listener methods for different events?

Maybe I am missing something out about Java's event handling but is there a neater way that I don't have to keep copy and paste the 3 methods plus creating so many different kind of event objects for every different kind of methods I want to dispatch?

Thanks!

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

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

发布评论

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

评论(2

蛮可爱 2024-11-17 08:56:43

我的事件处理策略是按可能适合您的类型发布。

我有一个代理,它可以检查侦听器中的注释,该注释将方法标记为侦听事件。使用这种方法,您只需要在想要处理特定类别的事件时添加方法。

interface Subscriber { // marker interface for OSGi
}

@interface SubscriberCallback { // marker annotation
}

class Broker {
    // uses reflections to find methods marked with @SubscriberCallback
    public void addSubscriber(Subscriber subscriber); 
    public void removeSubscriber(Subscriber subscriber);
    public <T> void publish(T... events);
}

class MyListener implements Subscriber {

@SubscriberCallback
public void onDogEvent(DogEvent... dogEvents) {
    // called for one or more dog events
}

@SubscriberCallback
public void onCatEvent(CatEvent catEvent) {
    // called for each CatEvent including subsclass of CatEvent.
}
}

The event handling strategy I have is to publish by type which may suit you.

I have a broker which can examine the listener for an annotation which marks a method as listening to events. Using this approach you only need to add methods when you want to handle a specific class of event.

interface Subscriber { // marker interface for OSGi
}

@interface SubscriberCallback { // marker annotation
}

class Broker {
    // uses reflections to find methods marked with @SubscriberCallback
    public void addSubscriber(Subscriber subscriber); 
    public void removeSubscriber(Subscriber subscriber);
    public <T> void publish(T... events);
}

class MyListener implements Subscriber {

@SubscriberCallback
public void onDogEvent(DogEvent... dogEvents) {
    // called for one or more dog events
}

@SubscriberCallback
public void onCatEvent(CatEvent catEvent) {
    // called for each CatEvent including subsclass of CatEvent.
}
}
审判长 2024-11-17 08:56:43

然后,如果我只想管理一种 CatEvent 事件,例如 Meow(和 EAT)

事件(MEOW 或 EAT)的“操作”应该是 CatEvent 中定义的数据。然后您的事件侦听代码将检查操作类型并进行适当的处​​理。

也许看一下 TableModelEvent 看看这是如何完成的。它使用同一事件处理“插入”、“删除”和“更新”事件。

您也可以基于 PropertyChangeListener 建模通用事件侦听器。 PropertyChangeListener 用于在 Swing 组件上的各种属性发生更改时处理事件。例如,当您调用 setForeground() 或 setBackground() 或 setFont() 或 setText() 或 setIcon 时。 PropertyChangeListener 使用 getName() 方法来确定哪个属性已更改。因此,对于上述方法,名称将是“前景”、“背景”、“字体”、“文本”、“图标”。有关如何使用属性更改侦听器的示例,请参阅如何使用属性更改侦听器可能会起作用。

在你的例子中,名字是“猫”和“狗”。仅当您创建的 GeneralEvent 可以包含与每个事件相关的信息(即“喵”和“吠”)时,此方法才有效。

Then if I want to manage just one kind of CatEvent event, say Meow, (and EAT)

The "action" of the event (MEOW or EAT) should be data defined in the CatEvent. Then your event listening code would check the action type and do the appropriate processing.

Maybe take a look at the TableModelEvent to see how this is done. It handles "insert", "delete" and "update" events using the same event.

Also you could probably model a general event listener based on the PropertyChangeListener. A PropertyChangeListener is used to handle events when various properties change on a Swing component. For example when you invoke setForeground() or setBackground() or setFont() or setText() or setIcon. The PropertyChangeListener uses a getName() method to determine which property has been changed. So for the above methods the names would be "foreground", "background", "font", "text", "icon". See How to Use Property Changes Listeners for an example of how this might work.

In your case the names would be "cat" and "dog". This approach would only work if the GeneralEvent you create can contain information that is relevant to each of your events (ie. "meow" and "bark").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文