hi all: I am working on two java programs where literally the only difference between the two are a beginning statement defining a 2-dimensional array. i have shortened the code substantially in the below example:
public class Qwerty {
/** this is what needs to be replaced */
private static char master[][] = {
QwertyKbd.lIndex,
QwertyKbd.lMiddle,
QwertyKbd.lRing,
QwertyKbd.lPinky,
/** and so forth in this fashion */
};
public static int[] analyze(String file) {
}
public static void split(char c) {
}
}
and the second class here:
public class Dvorak{
/** this is what needs to be replaced */
private static char master[][] = {
/** various definitions, eliminated for conciseness */
};
public static int[] analyze(String file) {
/** this is the same*/
}
public static void split(char c) {
/** this is the same*/
}
}
the question is how can i refactor to get one class where the only difference is the ‘master’ 2-d array? hook methods etc. would be stupid here. because there’s nothing that the program DOES that needs to be modified. what do you think?
You couldn’t do that if your classes are in diffent projects
But if they are parts of an project, you could create an abstract class, or superclass for all of your classes: Qwerty, Dvorak…
For example, you create an abstract
Then in your classes, you extends this abstract class
When you refactor the master field in CustomAbstract, all of master in other classes are refactored, too.
The problem of above code is that you couldnt use static field master, eventhough static method analyze, etc. May be you need all of methods and fields in your classes are static, you could use single skeleton pattern. In each class, you create a static method like that
Then instead of using Qwerty.analyze(String), you can use Qwerty.getInstance().analyze(String)
Hope this help.