GWT处理程序:最好有多个或一个?让它们处于不活动状态还是取消注册会更好吗? [英] GWT Handlers: is better to have many or a single one? Is it better to keep them inactive or unregister them?
问题描述
Button
。当用户点击按钮
时,我的应用程序会执行以下操作: - 亮点每当
标签
时,将其背景色设为绿色; 当用户将鼠标移动到高亮显示的标签上时,
/ code>,其背景变为深绿色,并且字体变成白色; 当用户将鼠标移出突出显示的 - code>,其背景恢复为绿色,字体恢复为黑色; 当用户点击突出显示的
标签
时,
- ,每一个
标签
被恢复到原始外观(即在用户点击按钮
之前)。
$ b $ p为了实现上述行为,我需要注册MouseOverHandler
,MouseOutHandler
和ClickHandler
为每个标签
。
假设我有一个实现所有3个处理程序的类
MyHandler
。从性能的角度来看,有什么更好的? - ,每一个
- 有一个
MyHandler
注册为MouseOverHandler
,MouseOutHandler
和ClickHandler
标签
- 将
MyHandler
的新实例注册为MouseOverHandler
,MouseOutHandler
和ClickHandler
对于每个标签
- highlights every
Label
by making its background color green; - when the user moves the mouse on an highlighted
Label
, its background becomes dark green, and the font turns white; - when the user moves the mouse out of an an highlighted
Label
, its background reverts green, and the font reverts to black; - when the user clicks on an highlighted
Label
, everyLabel
is reverted to the original appearance (i.e. before the user clicked theButton
). - have one single instance of
MyHandler
and register it asMouseOverHandler
,MouseOutHandler
, andClickHandler
for everyLabel
- register a new instance of
MyHandler
asMouseOverHandler
,MouseOutHandler
, andClickHandler
for everyLabel
标签时,
此外,用户点击突出显示的标签
,我不再对 MouseOverEvent
, MouseOutEvent
和 ClickEvent
来自标签。在这种情况下,取消注册 MyHandler
的实例会更好,还是让它们在下次用户点击 Button
?
没有理由使用不同的处理程序,它们全都相同。只需使用传入的事件来确定哪个标签正在与之交互并修改传入该事件的对象。
您的处理程序甚至不必知道多个标签存在。
当他们点击它时,您可以按照您的建议移除它,或者在您的处理程序开始时识别clicked状态,然后返回看看这个颜色)。
不要担心表现,差异绝对不可能检测到,做任何看起来更易读/可维护的东西。
p>就我个人而言,我认为我会让他们注册,以防我想以后再添加更多行为。
Let's assume that I have a GWT application with many instances of Label
(let's say hundreds of them), and a Button
. When the user click on the Button
, my application does the following:
In order to achieve the above behaviour I need to register MouseOverHandler
, MouseOutHandler
, and ClickHandler
for every Label
.
Let's say that I have a class MyHandler
implementing all 3 handlers. From performance perspective, what is better?
Moreover, after the user has clicked an highlighted Label
, I'm no longer interested in MouseOverEvent
, MouseOutEvent
, and ClickEvent
from the labels. In this situation, is it better to unregister the instance(s) of MyHandler
, or to keep them registered for the next time the user will click the Button
?
There is no reason to use different handlers, they all act the same. Just use the passed-in event to identify which label is being interacted with and modify the object passed into that event.
Your handler doesn't even have to know that multiple labels exist.
When they click on it you can either remove it as you suggest or identify the "clicked" state at the beginning of your handler and just return (Perhaps by looking at the color).
Don't worry about the performance, the difference will be absolutely impossible to detect, do whatever seems more readable/maintainable to you.
Personally I think I'd leave them registered in case I want to add more behavior later.
这篇关于GWT处理程序:最好有多个或一个?让它们处于不活动状态还是取消注册会更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!