问题描述
一般来说,java中有听众和处理程序的事件。我的意思是我不知不觉中使用它们,只要API中可用。我的问题是,在什么情况下我们使用监听器,在什么情况下我们使用处理程序进行事件?
他们有什么区别?特征??
我搜索了原因,找不到适合Java的解释。
听众和处理程序之间没有正式的区别。有些人可能会认为它们是可以互换的。然而对我来说,它们的意思略有不同。
听众是一个从源中订阅事件的对象。参看。通常,您可以让许多听众订阅每种类型的事件,并通过 添加 XyzListener
方法添加 / p>
示例: 。
处理程序是负责处理某些事件的对象。典型的情况是为特定事件/任务提供一个处理程序作为构造函数的参数,或者通过 set XyzHandler
设置处理程序
方法。换句话说,您通常对每种类型的事件都有一个处理程序。
示例: 在Java API中。
In general terms of java, there are listeners & handlers for events.
I mean I use them unknowingly, just whichever is available in the API.
My question is, in what case do we use listeners and in what case do we use handlers for events?
What's the difference between them? Characteristics??
I've searched for reasons and I couldn't find a proper explanation for Java.
There's no formally defined difference between listeners and handlers. Some people probably would probably argue that they are interchangeable. To me however, they have slightly different meaning.
A listener is an object that subscribes for events from a source. Cf. the observer pattern. Usually you can have many listeners subscribing for each type of event, and they are added through addXyzListener
methods.
Example: The MouseListener
in the Java API.
A handler is an object that is responsible for handling certain events. A typical scenario would be to provide a handler for a specific event/task as an argument to a constructor, or set the handler through a setXyzHandler
method. In other words, you usually have one handler for each type of event.
Example: The MemoryHandler
in the Java API.
这篇关于事件侦听器与Java中的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!