Imagine you have two methods which have exactly the same structure, but differ by methods they call inside. By 'structure' I mean same schemes of logical operators, or callbacks, or internal classes - i.e. everything that defines how the execution flows. Several use-cases possible:
1) both methods are similar but differ by internal calls they make (methodA and methodB)
void callIt(int a, int b) {
if (..) {
methodA();
}
}
void callIt(int a, int b) {
if (..) {
methodB();
}
}
2) methods are similar but differ by internal call parameters parameters (i.e. the only difference between the methods is the overloading of some calls inside (methodA) )
void callIt(int a, int b) {
if (..) {
methodA(a, b);
}
}
void callIt(String a, String) {
if (..) {
methodA(a, b);
}
}
Is it possible to simplify this code and avoid code repetition?
PS I use Java, but hope it doesn't matter.
Asnwer: Pete Belford gave an answer, if I got him correct - it is described more expanded here: Refactoring methods that use the same code but different types