Java 中更轻松的自定义事件处理
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的事件处理策略是按可能适合您的类型发布。
我有一个代理,它可以检查侦听器中的注释,该注释将方法标记为侦听事件。使用这种方法,您只需要在想要处理特定类别的事件时添加方法。
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.
事件(MEOW 或 EAT)的“操作”应该是 CatEvent 中定义的数据。然后您的事件侦听代码将检查操作类型并进行适当的处理。
也许看一下 TableModelEvent 看看这是如何完成的。它使用同一事件处理“插入”、“删除”和“更新”事件。
您也可以基于 PropertyChangeListener 建模通用事件侦听器。 PropertyChangeListener 用于在 Swing 组件上的各种属性发生更改时处理事件。例如,当您调用 setForeground() 或 setBackground() 或 setFont() 或 setText() 或 setIcon 时。 PropertyChangeListener 使用 getName() 方法来确定哪个属性已更改。因此,对于上述方法,名称将是“前景”、“背景”、“字体”、“文本”、“图标”。有关如何使用属性更改侦听器的示例,请参阅如何使用属性更改侦听器可能会起作用。
在你的例子中,名字是“猫”和“狗”。仅当您创建的 GeneralEvent 可以包含与每个事件相关的信息(即“喵”和“吠”)时,此方法才有效。
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").