Vaadin 中的单例
Singleton in Vaadin
在 Vaadin
中,将 Singleton pattern
用于 GUI(为我的情况附加 window)是一个好习惯吗?
My use-case is: One window cannot be displayed by other users while if
it is already in use by one user.
在那个用例之前,我只是将 window 添加到 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);
}
}
});
现在,它不允许许多用户显示 window(这是我想要的),但有 3 件事:
- 我真的觉得那是一种不好的做法,
- 显示window时,用户关闭浏览器不会让其他用户继续显示。
- 我真的是 Vaadin 的新手。
欢迎任何方法、建议。
谢谢。
这样不行。
任何 UI 个组件都分配给一个 Vaadin 会话。
所以你不能让一个 window 被多个 UI 实例使用。
处理您的用例的正确方法是为每个用户设置一个 window,然后将它们与某种事件总线或广播相结合,以便所有 windows 都得到更新.
为此,您需要在项目中启用推送,因为服务器必须向 "inactive" 用户发送更新。
https://vaadin.com/docs/v8/framework/articles/BroadcastingMessagesToOtherUsers.html
在 Vaadin
中,将 Singleton pattern
用于 GUI(为我的情况附加 window)是一个好习惯吗?
My use-case is: One window cannot be displayed by other users while if it is already in use by one user.
在那个用例之前,我只是将 window 添加到 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);
}
}
});
现在,它不允许许多用户显示 window(这是我想要的),但有 3 件事:
- 我真的觉得那是一种不好的做法,
- 显示window时,用户关闭浏览器不会让其他用户继续显示。
- 我真的是 Vaadin 的新手。
欢迎任何方法、建议。
谢谢。
这样不行。
任何 UI 个组件都分配给一个 Vaadin 会话。 所以你不能让一个 window 被多个 UI 实例使用。
处理您的用例的正确方法是为每个用户设置一个 window,然后将它们与某种事件总线或广播相结合,以便所有 windows 都得到更新.
为此,您需要在项目中启用推送,因为服务器必须向 "inactive" 用户发送更新。
https://vaadin.com/docs/v8/framework/articles/BroadcastingMessagesToOtherUsers.html