GWT处理程序:最好有多个或一个?让它们处于不活动状态还是取消注册会更好吗? [英] GWT Handlers: is better to have many or a single one? Is it better to keep them inactive or unregister them?

查看:92
本文介绍了GWT处理程序:最好有多个或一个?让它们处于不活动状态还是取消注册会更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个GWT应用程序,其中包含许多 Label (假设有数百个)实例,还有一个 Button 。当用户点击按钮时,我的应用程序会执行以下操作:


  • 亮点每当标签时,将其背景色设为绿色; 当用户将鼠标移动到高亮显示的标签上时,
  • / code>,其背景变为深绿色,并且字体变成白色; 当用户将鼠标移出突出显示的标签时,
  • code>,其背景恢复为绿色,字体恢复为黑色; 当用户点击突出显示的标签时,
  • ,每一个标签被恢复到原始外观(即在用户点击按钮之前)。



  • $ b $ p为了实现上述行为,我需要注册 MouseOverHandler MouseOutHandler ClickHandler 为每个标签



    假设我有一个实现所有3个处理程序的类 MyHandler 。从性能的角度来看,有什么更好的?


    • 有一个 MyHandler 注册为 MouseOverHandler MouseOutHandler ClickHandler 标签

    • MyHandler 的新实例注册为 MouseOverHandler MouseOutHandler ClickHandler 对于每个标签



    此外,用户点击突出显示的标签,我不再对 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:

    • 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, every Label is reverted to the original appearance (i.e. before the user clicked the Button).

    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?

    • have one single instance of MyHandler and register it as MouseOverHandler, MouseOutHandler, and ClickHandler for every Label
    • register a new instance of MyHandler as MouseOverHandler, MouseOutHandler, and ClickHandler for every Label

    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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆