I have a series of classes, A
,B
,C
... (several dozen in total) that share common code. There can be many instance of each class A
,B
,C
... . I'm planning to create a superclass, Abstract
, that will contain that code instead.
Problem is, the common stuff works on an object that is unique on a per-class (not per-instance) basis. This is currently solved by A
,B
,C
... each having a static field with the corresponding value. Obviously, when I refactor the functionality into Abstract
, this needs to be changed into something else.
In practice, it currently looks like this (note that the actual type is not String
, this is just for demonstrative purposes) :
public class A implements CommonInterface {
private static final String specificVar = "A";
@Override
public void common() {
specificVar.contains('');
}
}
public class B implements CommonInterface {
private static final String specificVar = "B";
@Override
public void common() {
specificVar.contains('');
}
}
The best idea I've come up with until now is to have a Map<Class<? extends Abstract>,K>
(where K
is the relevant type) static field in Abstract
, and A
,B
,C
... each containing a static initalization block that places the relevant value into the map. However, I'm not convinced this is the best that can be done.
Note that I'm not using any DI framework.
So, what would be the most concise, in terms of code contained in the subclasses, way to refactor the static fields in A
,B
,C
... handled by the common code, without sacrificing field access efficiency?