更新时间:2021-10-12 03:11:07
侦听器和处理程序之间没有正式定义的区别.有些人可能会争辩说它们是可以互换的.然而,对我来说,它们的含义略有不同.
There's no formally defined difference between listeners and handlers. Some people would probably argue that they are interchangeable. To me however, they have slightly different meaning.
监听器是一个从源订阅事件的对象.参见观察者模式.通常你可以有多个监听器订阅每种类型的事件,它们通过 addXyzListener
方法添加.
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.
示例: MouseListener
在 Java API 中.
Example: The MouseListener
in the Java API.
处理程序是负责处理特定事件的对象.一个典型的场景是为特定的事件/任务提供一个处理程序作为构造函数的参数,或者通过一个 setXyzHandler
set 处理程序> 方法.换句话说,对于每种类型的事件,您通常有一个处理程序.
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.
示例: MemoryHandler
在 Java API 中.
Example: The MemoryHandler
in the Java API.