我应该将文件对话框实现为单例吗?

发布于 2024-09-27 14:42:38 字数 121 浏览 1 评论 0原文

我正在开发一个基于 swing 的应用程序,其中使用了许多文件对话框?所以我说为什么不只创建一个 FileDialog 对象而不是所有这些实例并在整个项目中使用它呢?这是一个好的假设吗?这有任何性能改进吗?

谢谢

I'm developing a swing based application where I'm using many FileDialogs? So I say why not to make just one FileDialog object instead all of these instances and use it in the whole project? Is this a good assumption? does this have any performance improvement?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想你的星星会说话 2024-10-04 14:42:38

这是一个很好的用例示例,其中应用程序性能并不重要,问题实际上属于问题解决的过早优化类别。为什么?使用 FileDialog 意味着您正在与用户交互,即使用户熟练掌握快捷键功夫,也会比应用程序慢许多数量级。快速用户可以在一分钟内打开、使用和关闭多少个文件对话框?说一打吧。你不需要关心一分钟内来来往往的十几个物体。甚至不应该出现在你的雷达上。将精力用在其他地方。事实上,您应该每次都创建一个新对象并避免任何令人头疼的缓存问题。

This is a great example of a use case where application performance doesn't really matter and the question actually falls into the premature optimization class of problem solving. Why? Using FileDialog means that you are interacting with the user who, even if skilled beyond skilled with shortcut key Kung Fu, will be many orders of magnitude slower than the application. How many FileDialogs could a speedy user open, use and close in one minute? Say a dozen. You should not need to care about a dozen objects coming and going in one minute. Shouldn't even appear on your radar. Use your energies elsewhere. In fact, you should create a new object every time and avoid any caching headaches.

思慕 2024-10-04 14:42:38

我将创建一个静态 FileDialog 类,每次需要打开一个新的 FileDialog 实例时,它都会生成一个新的 FileDialog 实例,而不是在应用程序中共享 Singleton 实例。

这将使您免去试图弄清楚您是否正在从对话框中读取正确的路径,或者是否有人打开了对话框并选择了一个新路径,而现在您正在引用该新路径而不是最初选择的路径的麻烦。路径等...

I would make a static FileDialog class that generates a new instance of the FileDialog each time a new one needs open rather than sharing a Singleton instance across the application.

That'll save you the headache of trying to figure out if you're reading the correct path from the dialog box or if somebody has opened the dialog and picked a new path and now you're referencing that new path rather than the originally selected path, etc...

软的没边 2024-10-04 14:42:38

为什么要实现为单例?您实际上可以验证永远不会出现显示两个文件对话框吗?

最好将其作为常规课程进行;您不想设置一些限制,这些限制可能会成为以后的痛点。

您的应用程序不会因对文件对话框的数百万次调用而严重超载,谁知道呢,也许有一天拥有两个文件对话框将是正确的解决方案。即使您不同时显示它们,也许在“源”对话框中保存历史记录并在“目标”对话框中拥有单独的历史记录对于文件传输程序来说是一件幸事。

Why implement is as a Singleton? Can you actually verify that displaying two file dialogs will never occur?

Better to have it as a regular class; you don't want to build in limitations which could become pain points later.

It isn't like your application is going to be critically overloaded by millions of calls to the file dialog, and who knows, perhaps someday it will be the right solution to have two file dialogs. Even if you don't display them both at the same time, perhaps holding the history in a "source" dialog and having a separate history in the "destination" dialog would be a blessing in a file transfer program.

偏爱你一生 2024-10-04 14:42:38

忘记性能/速度。这里没关系。语义很重要。重复使用相同的文件对话框可能会免费为您提供一些东西。对话框每次都会在同一目录中启动吗?如果是同一个实例,就会出现这种情况。如果您要创建新对话框,则必须自行设置启动目录。

另外,为什么无法创建多个实例呢?只需在您的框架中创建一个实例成员即可完成。

Forget performance/speedyness. It doesn't matter here. Semantics matter. Reusing the same file dialog may give you things for free. Will the dialog start in the same directory every time? It will if it is the same instance. If you are creating new dialogs you will have to set startup dir your self.

Also why make it impossible to create more than one instance? Just make an instance member in your frame and be done with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文