Where do you keep Constants used throughout your application?

前端 未结 5 1143
小蘑菇
小蘑菇 2021-02-05 03:59

Is interface an acceptable place to store my

public static final Foo bar

Do you extrapolate them to be read from outside of the program? Do you

相关标签:
5条回答
  • 2021-02-05 04:06

    This is bad practice. Classes are intended to be independent from one another therefore you should avoid global variables at all costs. A more realistic approach is to have a config file, usually in JSON, YAML, or XML file format, and have your program read from this file on start up.

    0 讨论(0)
  • 2021-02-05 04:12

    I would use an abstract final class since it would be a more explicit declaration of the modifiers as opposed to using an interface. But, like Michael said, they should be logically grouped and be part of an existing class or interface if they semantically belong there.

    0 讨论(0)
  • 2021-02-05 04:18

    I'd put each constant into the class or interface it's most closely related to (e.g. because it will be use by its methods).

    A very seductive but ultimately very foolish idea is to have one "constants class" (or interface) that contains all constants used in the application. This looks "neat" at first glance, but is not good for maintainability because you want to group things by the functionality they implement, not by technical details like being constants (would you put all interfaces into a dedicated package? All abstract classes?).

    The idea is also foolish because any change to that class/interface then (because of constant inlining) requires all classes that use any of the constants to be rebuilt - i.e. pretty much the entire app. So the bigger the app gets, the more frequently you need such a full rebuild, and the longer it takes. I worked on such a project, where this issue led to a 15 minute pause about every other day for every developer...

    0 讨论(0)
  • 2021-02-05 04:25

    If you are talking about a simple application the Constants class approach is fine:

    public class Constants {
        private Constants() {} // no way to instantiate this class
        public static final String MY_VAL = "123";
    }
    

    If you're building a larger app you should be using dependency injection, take a look at How can I inject a property value into a Spring Bean which was configured using annotations?

    0 讨论(0)
  • 2021-02-05 04:25

    I've used Abdullah Jibaly's approach but using nested classes, which provides a nice way for grouping consts. I've seen this go 3 levels deep and if good names are chosen, it can still work quite well.

    One might choose to use final classes for this instead of interface classes to avoid violating Item #19 on Josh Bloch's list (Effective Java 2nd edition).

    public final class Const {
        private Const() {} // prevent instantiation
    
        /** Group1 constants pertain to blah blah blah... */
        public static final class Group1 {
            private Group1() {} // prevent instantiation
    
            public static final int MY_VAL_1 = 1;
            public static final int MY_VAL_2 = 42;
        }
    
        /** Group2 constants pertain to blah blah blah... */
        public static final class Group2 {
            private Group2() {} // prevent instantiation
    
            public static final String MY_ALPHA_VAL = "A";
            public static final String MY_ALPHA_VAL = "B";
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题