首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Singleton in Vaadin

Singleton in Vaadin
EN

Stack Overflow用户
提问于 2018-11-30 09:25:23
回答 1查看 216关注 0票数 3

Singleton pattern中,在Vaadin中使用GUI(为我的情况附加一个窗口)是一个很好的实践吗?

我的用例是:一个窗口不能由其他用户显示,如果一个用户已经在使用它。

在这个用例之前,我只是简单地向gui添加了窗口,如下所示:

代码语言:javascript
复制
  this.communicationWindow = new CommunicationConfigWindow();
  this.configSettingsButton.addClickListener( e -> {
     if ( !UI.getCurrent().getWindows().contains( this.communicationWindow )
              && this.communicationWindow != null )
     {
        this.communicationWindow.init( this.config );
        this.getUI().addWindow( this.communicationWindow );
     }
  } );

因为我希望它只由一个用户显示,而不是

代码语言:javascript
复制
this.communicationWindow = new CommunicationConfigWindow();

我只需像下面这样将其转换为singleton,并添加try/catch块;

代码语言:javascript
复制
this.communicationWindow = CommunicationConfigWindow.getInstance();
this.communicationWindow = new CommunicationConfigWindow();
this.configSettingsButton.addClickListener( e -> {
    if ( !UI.getCurrent().getWindows().contains( this.communicationWindow )
                  && this.communicationWindow != null )
    {
       this.communicationWindow.init( this.config );
       try
       {
           this.getUI().addWindow( this.communicationWindow );
       }
       catch(IllegalArgumentException ex)
       {
           Notification.show( "Configuration invalid", Type.WARNING_MESSAGE);
       }
    }
});

现在,它不允许许多用户显示该窗口(这正是我想要的),但是有三件事:

  1. 我真的觉得这是个糟糕的练习,
  2. 当窗口被显示时,用户关闭浏览器--它将不让其他用户继续显示。
  3. 我真的是瓦丁的新手。

欢迎任何方法和建议。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-30 10:34:36

这样不行的。

任何UI组件都分配给一个Vaadin会话。因此,您不能让多个UI实例使用一个窗口。

处理用例的正确方法是为每个用户设置一个窗口,然后将它们与一些事件总线或广播结合起来,以便更新所有窗口。

为此,您需要在项目中启用推送,因为服务器必须向“非活动”用户发送更新。

https://vaadin.com/docs/v8/framework/articles/BroadcastingMessagesToOtherUsers.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53554633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档