We are in the process of refactoring some code. There is one feature that we have developed in one project that we would like to use in other projects. We extract the basis of this function and make it a full-fledged project, which can then be imported according to the current project and others. These efforts were relatively straightforward, but we have one headache.
When the structure in question was originally developed, we decided to keep many constant values โโdefined as static fields in one class. Over time, this list of static members has grown. The class is used in so many places in our code. In our current refactoring, we will raise some members of this class to a new framework, but leave others in place. Our headache is to retrieve the members of this classโs foundation for use in our new project and, more specifically, how we should access those retrieved members in our existing code.
We know that we can have our existing Constants subclass of this new Constants class and it inherits all the static static members. This would allow us to make the change without touching the code that these members use to change the class name in the static link. However, the rigid connection inherent in this choice does not seem to be correct.
before:
public class ConstantsA {
public static final String CONSTANT1 = "constant.1";
public static final String CONSTANT2 = "constant.2";
public static final String CONSTANT3 = "constant.3";
}
after
public class ConstantsA extends ConstantsB {
public static final String CONSTANT1 = "constant.1";
}
public class ConstantsB {
public static final String CONSTANT2 = "constant.2";
public static final String CONSTANT3 = "constant.3";
}
In our existing code branch, all of the above will be available as follows:
ConstantsA.CONSTANT2
I would like to ask for arguments about whether this is โacceptableโ and / or what are best practices.
source
share