Java应用程序代码结构
我的 Java 应用程序不是很大,但我想以“正确的方式”完成它。寻找建议以免造成大混乱...
我正在使用 Swing、Swing 应用程序框架和 H2 数据库。
它是一个具有不同选项卡的单框架应用程序。大多数选项卡将反映数据库中数据的某些方面。他们中的一些人将能够改变它。某些选项卡将具有重定向到另一个选项卡的按钮或操作......基本上就是这样。
我想保持东西干净,相当便携和灵活(例如可以启用禁用某些选项卡或轻松添加一些选项卡)。
以下是问题:
1)当数据库内容发生变化时如何让选项卡“刷新”?实现一些监听器接口?我应该定义该接口还是有一些已经存在的东西我可以重用?
2)如何将不同的选项卡“链接”在一起,以便很容易从一个选项卡跳转到另一个选项卡(并且可能需要携带一些参数来将目标选项卡设置到右侧的“视图”)
3)如果我设置一些首选项应用程序另一部分中的一些过滤器,如何与整个应用程序共享此类设置?
当我问“如何”时,我真正的意思是“最好的方法是什么”......我已经可以让它工作了......只是寻找合适的工具来使用,设计模式等。
My Java application is not very big, but I would like to do it the "right way". Looking for suggestions to not make a big mess...
I am using Swing, the Swing Application Framework and H2 database.
It is a single frame application with different tabs. Most tabs will reflect some aspects of the data in the database. Some of them will be able to change it. Some tabs will have buttons or actions that will redirect to another tab... that's basically it.
I would like to keep things clean, rather portable and flexible (can enable disable some tabs for example or add some easily).
Here are the questions :
1) How to have tabs "refreshed" when the database content changes ? Implement some Listener interface ? Should I define that interface or there is something already existing I can reuse ?
2) How to "link" the different tabs together so that it is easy to jump from one to another (and may have to carry some argument to setup the destination tab to the right "view")
3) If I set some preference with some filters in another part of the application, how do I share such settings with the whole application ?
And when I ask "how to", I really mean "what is the best way"... I can get that working already... just looking for the right tool to use, design pattern or such.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GUI 应用程序构建有不同的策略。 这里一些关于的文章。
There are different strategies of gui application building. Here some article about.
如果使用 swing,您可以使用选项卡式窗格来实现表格布局。
实现一个侦听器(如您计划的那样)。看看 Java 中的可观察接口。
您可以实现一个单例(例如 PreferenceManager 类)来保存首选项并跨应用程序访问它以访问属性。
If using swing, you can use tabbed pane to have a tabular layout.
Implement a listener (as you are planning). Have a look at observable interface in Java.
You can implement a singleton(eg PreferenceManager class) to hold the preference and access it across the application to access properties.
您是否真的想要一种推送模型,其中服务器必须保持与每个客户端的连接,然后在发生任何变化时更新所有客户端?这对于少数客户来说是可行的,但很快就会出现问题。最好使用拉模型,客户端手动(定期?)请求更新。这也是对数据库中的 last_modified 字段使用乐观锁定的好方法。尝试更新,如果自上次重新加载以来发生了更改,请警告用户并允许他们覆盖/合并。
一般来说,对于 Swing,最佳实践是将与 GUI 无关的所有内容分解到触发事件的核心类(“业务逻辑”)中。将所有 Swing 类注册为适当的事件侦听器并相应地更新 gui(SwingUtilities.invokeLater() 是您的朋友)。
Do you really want a push model where the server has to maintain a connection to every client and then update them all when anything changes? This is feasible with a handful of clients, but quickly becomes problematic. It's probably better to use a pull model, where the client manually (periodically?) asks for updates. This is also a good way to use optimistic locking with a last_modified field in the DB. Attempt an update, if it's changed since you last reloaded, warn the user and allow them to overwrite / merge.
As for Swing in general, best practice is to factor everything out that's not GUI related into core classes ('business logic') that fire off events. Register all your Swing classes as the appropriate event listener and update the gui appropriately (SwingUtilities.invokeLater() is your friend here).