0

I'm a Java web beginner. Today I came onto the following scenario:

I have several classes with no inheritance relationship like ClassA and ClassB

public class ClassA {
    private String a1;
    private String a2;
}

public class ClassB {
    private String b1;
    private String b2;
    private String b3;
}

Functions to process them are exactly the same, except for the input parameter type and some get methods.

public void function functionA(ClassA a) {
    ...
    Object[] obj = new Object[]{a.getA2()};
    ...
    
}

public void function functionB(ClassB b) {
    ...
    Object[] obj = new Object[]{b.getB1(),b.getB2(),b.getB3()};
    ...
    
}

Apparently, writing in this way causes code duplication and low scalability, I've tried to make it tidy, but I have no idea about handling with the input type difference.

Could any one tell me how to extract the common logic of functionA and functionB?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Not sure what this has to do with Spring or Spring Boot. Seems like more of a general design question. Extract the common part to a method, call that with the needed input. – M. Deinum Feb 21 '23 at 15:37

2 Answers2

1

Create an abstraction to extract common functionality - define an interface.

public interface MyInterface {

  Object[] getValues();
}

Have your classes implement it. Then refactor your handler method to accept MyInterface.

public void function(MyInterface myInterface) {
  Object[] values = myInterface.getValues();
  //handle
}

I would also suggest to read this question - What does it mean to "program to an interface"? - it has one of the best explanations on the topic.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
0

If I understand your question, I think you are looking for something akin to interfaces. I'm gonna make some assumptions but it looks like you are trying to put all the fields of a class in an Object[] array and are looking for a way to do so without having many methods with different input parameters..

So what you could do is create an interface IMyInterface (sorry for the unoriginal name):

public interface IMyInterface {
    public Object[] toObjectArray();
}

Your ClassA and ClassB could then both implement this interface:

public class ClassA implements IMyInterface {
    private String a1;
    private String a2;

    @Override
    public Object[] toObjectArray() {
        return new Object[]{ getA1(), getA2() };
    }
}

public class ClassB implements IMyInterface {
    private String b1;
    private String b2;
    private String b3;

    @Override
    public Object[] toObjectArray() {
        return new Object[]{ getB1(), getB2(), getB3() };
    }
}

This would make the ClassA and ClassB in control of how the object array is built up.

Then, in the rest of your application, you can simply use this IMyInterface instance to either accept parameters of type ClassA or ClassB:

public void someFunction(IMyInterface item) {
    Object[] obj = item.toObjectArray();
    // Rest of your code..
}

I'm not sure if that's exactly what you're looking for.

nbokmans
  • 5,492
  • 4
  • 35
  • 59