So I have two identical classes, say ClassA and ClassB. In both classes, they operate on the same kind of data classes, and the method bodies are exactly the same. The only difference is that the imported classes, which identically named in both ClassA and `ClassB, come from different packages/projects. That is,
//Class A
package packageA;
import com.project.google.cool.Message;
import com.project.google.cool.CoolClass;
public class ClassA {
//this is a static class
public static void method(Message m) {
m.setType("cool message");
m.setObj(new CoolClass());
}
.
.
.
//etc same for other methods
}
//Class B
package packageB;
import com.project.microsoft.cool.Message;
import com.project.microsoft.cool.CoolClass;
public class ClassB {
// this is a static class
public static void method(Message m) {
m.setType("cool message");
m.setObj(new CoolClass());
}
.
.
.
//etc same for other methods
}
The classes from com.project.google/microsoft are autogenerated and there's nothing I can do to change them, including renaming.
I've explored creating a generic interface, where both ClassA and ClassB implement said interface, but I dont think I got an implementation working. As is, SonarQube reported "code duplication", and I was wondering if there's a design pattern available to tackle situations like this.
The actual caller of the methods (e.g. method) has the responsibility of handing off the correct data type, either from project "google" or "microsoft".