加载设置 - 最佳实践
我现在正在第一个真正的应用程序中添加用户设置。我正在使用 Java 并且非常面向对象(并试图保持这种方式),所以这是我的想法:
- 在
main()
中加载所有内容并 将其全部“下线”传递给 必需的对象(数组) - 与上面相同,但只需传递 包含数据的对象 该行将
- 每个单独的设置加载为 各个类别中都需要。
我了解每种方法的一些基本优点和缺点(即时间与大小),但我正在寻找一些外部输入来了解他们过去成功使用的做法。
I'm at the point in my first real application where I am adding in the user settings. I'm using Java and being very OO (and trying to keep it that way) so here are my ideas:
- Load everything in the
main()
and
pass it all 'down the line' to the
required objects (array) - Same as above, but just pass the
object that contains the data down
the line - Load each individual setting as
needed within the various classes.
I understand some of the basic pros and cons to each method (i.e. time vs. size) but I'm looking for some outside input as to what practices they've successfully used in the past.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有人应该支持所谓的 Java 标准,即 Preferences API...它的最新版本位于 JDK6。 编辑添加,因为作者似乎精通XML,所以这更比以前合适。我想你可以使用 XML juju
属性
,如果你有兴趣的话。相关SO:首选项API与Apache解决方案,主偏好类是一个好主意吗?
(嗯,这就是全部我愿意站起来。)
Someone should stand up for the purported Java standard, the Preferences API... and it's most recent incarnation in JDK6. Edited to add, since the author seems to savvy XML, this is more appropriate than before. Thought I believe you can work XML juju with
Properties
too, should the spirit take you.Related on SO: Preferences API vs. Apache solution, Is a master preferences class a good idea?
(well, that's about all the standing up I'm willing to do.)
使用 SettingsManager 类或类似的类来抽象获取所有设置数据。在代码中需要设置的每个点,您都会查询 SettingsManager 类 - 类似于:
然后,您将如何获取设置的所有逻辑委托给这个单个管理器类,您可以根据需要更改/优化其实现。例如,您可以实现 SettingsManager 从配置文件、数据库或其他一些数据存储中获取设置,定期刷新设置,处理检索成本高昂的设置的缓存等。使用设置的代码仍然很幸福不知道所有这些实施决定。
为了获得最大的灵活性,您可以使用接口而不是实际的类,并让不同的设置管理器实现该接口:您可以根据需要在某个中心点将它们换入和换出,而无需更改底层代码。
在 .NET 中,有一组相当丰富的现有配置类(在 System.Configuration 中)提供此类功能,并且效果非常好。
我不确定 Java 的等效模式,但它是一个很好的模式。
Use a SettingsManager class or something similar that is used to abstract getting all settings data. At each point in the code where you need a setting you query the SettingsManager class - something like:
You then delegate all of the logic for how settings are fetched to this single manager class, whose implementation you can change / optimize as needed. For instance, you could implement the SettingsManager to fetch settings from a config file, or a database, or some other data store, periodically refresh the settings, handle caching of settings that are expensive to retrieve, etc. The code using the settings remains blissfully unaware of all of these implementaton decisions.
For maximum flexibility you can use an interface instead of an actual class, and have different setting managers implement the interface: you can swap them in and out as needed at some central point without having to change the underlying code at all.
In .NET there is a fairly rich set of existing configuration classes (in the System.Configuration) namespace that provide this sort of thing, and it works out quite well.
I'm not sure of the Java equivalent, but it's a good pattern.
由于配置/设置通常加载一次(在启动时;或者可能在程序运行时加载几次。无论如何,我们不是在谈论非常频繁/耗时的过程),因此我更喜欢简单而不是效率。
这就排除了选项号 (3)。配置加载将分散在各处。
我不完全确定您的列表中的 (1) 和 (2) 之间有什么区别。 (1) 是否意味着“传递离散参数”,(2) 是否意味着“传递包含整个配置的对象”?如果是这样,我更喜欢(2)而不是(1)。
这里的经验法则是你应该让事情简单而集中。在一个地方读取配置的优点是,如果配置源在某个时刻发生变化,它可以让您更好地控制。
Since configuration / settings are typically loaded once (at startup; or maybe a few times during the program's runtime. In any way, we're not talking about a very frequent / time-consuming process), I would prefer simplicity over efficiency.
That rules out option number (3). Configuration-loading will be scattered all over the place.
I'm not entirely sure what the difference is between (1) and (2) in your list. Does (1) mean "passing discreet parameters" and (2) mean "passing an object containing the entire configuration"? If so, I'd prefer (2) over (1).
The rule of thumb here is that you should keep things simple and concentrated. The advantage of reading configuration in one place is that it gives you better control in case the source of the configuration changes at some point.
这里是关于 Properties 类的教程。来自 Javadocs(属性):
本教程提供了以下典型用法的实例化示例:
当然,您也可以使用基于文件的存储和 XML 或 YAML 解析器相当直接地推出自己的系统。祝你好运!
Here is a tutorial on the Properties class. From the Javadocs (Properties):
The tutorial gives the following example instantiation for a typical usage:
You could, of course, also roll your own system fairly directly using a file-based store and an XML or YAML parser. Good luck!
我们最近开始使用 JSR-330 依赖注入(使用 SVN 中的 Guice),并发现可以读取属性文件(或任何其他映射)并将其绑定到启动代码中模块中的 Guice 中,以便
字符串当调用该特定代码时,注入了与该键对应的值。这是我见过的解决这个问题的最优雅的方法!
您不必在代码中拖拽配置对象,也不必在代码的每个角落调用各种神奇方法来获取值 - 您只需向 Guice 提及您需要它,它就在那里。
注意:我看过 Guice、Weld(基于 Seam)和 Spring,它们都提供注入,因为我们希望在自己的代码中使用 JSR-330,而且我目前最喜欢 Guice。我认为原因是 Guice 的绑定是最清晰的,而不是 Weld 发生的幕后魔力。
We have recently started using JSR-330 dependency injection (using Guice from SVN) and found that it was possible to read in a Properties file (or any other map) and bind it inside Guice in the module in the startup code so that the
string was injected with the value corresponding to the key when that particular code was called. This is the most elegant way I have ever seen for solving this problem!
You do not have to haul configuration objects around your code or sprinkle all kinds of magic method calls in each and every corner of the code to get the values - you just mention to Guice you need it, and it is there.
Note: I've had a look at Guice, Weld (Seam-based) and Spring which all provide injection, because we want JSR-330 in our own code, and I like Guice the best currently. I think the reason is because Guice is the clearest in its bindings as opposed to the under-the-hood magic happening with Weld.