简体   繁体   English

设置包装器类是一种好习惯吗?

[英]Is setting wrapper class a good practice?

I was thinking how to wrap all my settings and for example I use once or even more. 我在考虑如何包装所有设置,例如,我使用了一次甚至更多次。 Let's think a bit abstractive. 让我们考虑一下抽象性。

class WholeApplicationSettingsInHere{
    private static boolean setting1, setting2;

    WholeApplicationSettingsInHere(){set defaults}

    public static boolean getSetting1(){return setting1}
    public static void setSetting1(setting1){set setting1}

    public static boolean getSetting2(){return setting2}
    public static void setSetting2(setting2){set setting2}

    ...
}

class One(){
/*No fields in here, except private fields*/
    methodOne(){use variables from WholeApplicationSettingsInHere class}
    methodTwo(){}
}

class Two(){
/*No fields in here, except private fields*/
    methodOne(){use variables from WholeApplicationSettingsInHere class}
    methodTwo(){}
}

My question is if it will be a good practice to do things that way? 我的问题是,这样做是否是一种好习惯?

You should the PreferenceManager and SharedPreferences to store and retrieve user preferences. 您应该使用PreferenceManagerSharedPreferences来存储和检索用户首选项。 Using static booleans in a class doesn't really seem like a great idea. 在类中使用静态布尔值似乎并不是一个好主意。

Just create a simple class with your methods, for example: 只需使用您的方法创建一个简单的类,例如:

public class PreferencesHelper {

    private PreferencesHelper() {} // No instantiation

    public static boolean getSetting1(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREFERENCE_KEY, defaultValue);
    }

    public static void setSetting1(Context context, boolean value) {
       Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
       editor.putBoolean(PREFERENCE_KEY, value);
       editor.apply();
    }

}

And finally, to answer your question: Yes, it can be good practice, if you do something like the above-mentioned code. 最后,回答您的问题:是的,如果您执行上述代码,则可能是一个好习惯。 By doing so, you make it impossible to have preference keys scattered across your application, and it makes your code easier to read. 这样,您就不可能使首选项键分散在整个应用程序中,并且使代码更易于阅读。

Having said that, if you're ever in a situation where you need to set multiple preferences at once, it's a bit of a waste having to call setSetting1() , setSetting2() , setSetting3() , etc., as you're recreating objects over and over again. 话虽如此,如果您曾经需要一次设置多个首选项的情况,那么在您需要调用setSetting1()setSetting2()setSetting3()等情况下会有些浪费。一遍又一遍地重新创建对象。 In such cases, simply get the Edtior object, put all your changes, and apply the changes. 在这种情况下,只需获取Edtior对象,放置所有更改,然后应用更改。

It's pretty hard to answer "is this a good practice" because there isn't one definitive answer. 很难回答“这是一个好习惯”,因为没有一个明确的答案。 It depends entirely on your context. 这完全取决于您的上下文。 You might want to provide an MCVE that more accurately reflects exactly what you're doing. 您可能需要提供一个MCVE ,它可以更准确地准确反映您在做什么。

I will say that making those variables and methods static is a pretty horrible idea- what happens down the road if you want two instances of your settings class? 我要说的是,将那些变量和方法静态化是一个非常可怕的想法-如果您想要设置类的两个实例,接下来会发生什么?

And before you say "I'll only ever need one instance of the settings"- you can't know that ahead of time. 而且,在您说“我只需要一个设置实例”之前,您可能无法提前知道。 This is a misuse of the static keyword. 这是对static关键字的滥用。

And at that point- why not just use an instance of the Properties class? 到那时,为什么不只使用Properties类的实例呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM