I'm developing a swing app which suits the MVC pattern and I'm wondering about the best place to store settings such as width/height, xml files location... Should those settings be avaiable also only through the Model? Should I use a global static class? A singleton?
我正在开发一个适合MVC模式的swing应用程序,我想知道存储设置的最佳位置,例如宽度/高度,xml文件位置......这些设置是否也可以通过模型进行设置?我应该使用全局静态类吗?单身人士?
Thanks in advance
提前致谢
3 个解决方案
#1
I'd suggest java.util.prefs
.
Preferences
.
我建议使用java.util.prefs.Preferences。
Then you don't have to invent anything.
然后你不必发明任何东西。
#2
I actually used the Swing Application Framework in NetBeans with great success here which deals with it in a way where you dont have to worry too much about design patterns :)
我实际上在NetBeans中使用了Swing应用程序框架并取得了巨大的成功,它以一种您不必过多担心设计模式的方式处理它
Before than I'd normally store the window properties in properties files and I had a separate config model/service that I injected where it was needed to retrieve the properties when re-creating windows.
之前我通常将窗口属性存储在属性文件中,并且我有一个单独的配置模型/服务,我注入了重新创建窗口时检索属性所需的位置。
I cannot see why it would need to be a Singleton. Probably an anti-pattern in this case.
我不明白为什么它需要成为一个单身人士。在这种情况下可能是一种反模式。
#3
It depends on what settings you're talking about.
这取决于你所谈论的设置。
Width
and Height
sounds like properties that are only relevant to the View part of your project, and should thus be stored within it. Xml file location
sounds more like a Model concern.
宽度和高度听起来像只与项目的“查看”部分相关的属性,因此应存储在其中。 Xml文件位置听起来更像是模型问题。
When developing in Java (which I quite rarely do...) I tend to set up a class named Settings
in which I store whatever I need as private fields, with getters and/or setters where it's needed. In an MVC pattern, I'd have one settings class in each section, and (if necessary, but rather not) one "global" settings class. But if you're concerned with pattern conformity, make sure that each setting is only available where it's needed.
当用Java开发时(我很少做...)我倾向于设置一个名为Settings的类,在其中我存储我需要的任何私有字段,其中需要getter和/或setter。在MVC模式中,我在每个部分中都有一个设置类,并且(如果需要,但不是)一个“全局”设置类。但是,如果您关注模式符合性,请确保每个设置仅在需要时才可用。
#1
I'd suggest java.util.prefs
.
Preferences
.
我建议使用java.util.prefs.Preferences。
Then you don't have to invent anything.
然后你不必发明任何东西。
#2
I actually used the Swing Application Framework in NetBeans with great success here which deals with it in a way where you dont have to worry too much about design patterns :)
我实际上在NetBeans中使用了Swing应用程序框架并取得了巨大的成功,它以一种您不必过多担心设计模式的方式处理它
Before than I'd normally store the window properties in properties files and I had a separate config model/service that I injected where it was needed to retrieve the properties when re-creating windows.
之前我通常将窗口属性存储在属性文件中,并且我有一个单独的配置模型/服务,我注入了重新创建窗口时检索属性所需的位置。
I cannot see why it would need to be a Singleton. Probably an anti-pattern in this case.
我不明白为什么它需要成为一个单身人士。在这种情况下可能是一种反模式。
#3
It depends on what settings you're talking about.
这取决于你所谈论的设置。
Width
and Height
sounds like properties that are only relevant to the View part of your project, and should thus be stored within it. Xml file location
sounds more like a Model concern.
宽度和高度听起来像只与项目的“查看”部分相关的属性,因此应存储在其中。 Xml文件位置听起来更像是模型问题。
When developing in Java (which I quite rarely do...) I tend to set up a class named Settings
in which I store whatever I need as private fields, with getters and/or setters where it's needed. In an MVC pattern, I'd have one settings class in each section, and (if necessary, but rather not) one "global" settings class. But if you're concerned with pattern conformity, make sure that each setting is only available where it's needed.
当用Java开发时(我很少做...)我倾向于设置一个名为Settings的类,在其中我存储我需要的任何私有字段,其中需要getter和/或setter。在MVC模式中,我在每个部分中都有一个设置类,并且(如果需要,但不是)一个“全局”设置类。但是,如果您关注模式符合性,请确保每个设置仅在需要时才可用。