在Singleton pattern中,在Vaadin中使用GUI(为我的情况附加一个窗口)是一个很好的实践吗?
我的用例是:一个窗口不能由其他用户显示,如果一个用户已经在使用它。
在这个用例之前,我只是简单地向gui添加了窗口,如下所示:
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 );
}
} );因为我希望它只由一个用户显示,而不是
this.communicationWindow = new CommunicationConfigWindow();我只需像下面这样将其转换为singleton,并添加try/catch块;
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);
}
}
});现在,它不允许许多用户显示该窗口(这正是我想要的),但是有三件事:
欢迎任何方法和建议。
谢谢。
发布于 2018-11-30 10:34:36
这样不行的。
任何UI组件都分配给一个Vaadin会话。因此,您不能让多个UI实例使用一个窗口。
处理用例的正确方法是为每个用户设置一个窗口,然后将它们与一些事件总线或广播结合起来,以便更新所有窗口。
为此,您需要在项目中启用推送,因为服务器必须向“非活动”用户发送更新。
https://vaadin.com/docs/v8/framework/articles/BroadcastingMessagesToOtherUsers.html
https://stackoverflow.com/questions/53554633
复制相似问题