0

I have multiple child objects that extend the PERSON type. Then, for every type of person I have different methods that are similar to each other, and the only difference is the class subtype

-For example:

public void handlePerson1(final String request) {

        PersonExample<Person1> personExample= new PersonExample<>(Person1.class);
        OtherPersonExample<Person1> otherPersonExample = new OtherPersonExample<>(Person1.class);

public void handlePerson2(final String request) {

        PersonExample<Person2> personExample= new PersonExample<>(Person2.class);
        OtherPersonExample<Person2> otherPersonExample = new OtherPersonExample<>(Person2.class);

I tried to make the main class extend a generic type so that I could initiate the remaining classes using the type I needed, because with other functions, I have something very similar that worked out. For example, in the code above, the personExample:

public class PersonExample<T extends Person> {
    private final Class<T> classType;

    //Get Class Type
    public PersonExample(final Class<T> classType) {
        this.classType = classType;
    }

    public void handleFunction(Something<T> something) {
              //SOMETHING CAN HAVE A PERSON1, OR PERSON2, etc...

    }

Can someone help me transform handlePerson1, 2 etc.. in one singular function?

  • 1
    Your example seems over-minimized to me - I miss the usage of `request` argument in concrete `handlePersonX` methods. Without it, simple `PersonExample personExample= new PersonExample<>(classType);` would suffice which is probably not what you want. Is the `String request` a potential source of many different subtypes? It would point us to factory or some other creational design pattern (you may expect it from `Something` class?). Also please specify your Java version since in modern Java versions it can be done in more convenient way (patterns in switch). – Tomáš Záluský Mar 23 '23 at 20:09
  • First of all thank you for you answer. I am using Java 11, and my problem is in the handle person functions, the code of the PersonExample is already working and its ok. final String request is a JSON that will be converted to a JAVA object that contains an T[] with elements of person1, or person2, or person3, so yeah, request can have different types of person in. The handlefunction already accepts every kind of person, but the problem is that I have different endpoints that are the same and call handlePerson1, 2 etc. and that functions are the same, the difference is the classtype. – Renato Quelhas Mar 24 '23 at 15:45
  • Please show more code. The `request` arguments are still unused. If request can contain mixed information of `Person1`, `Person2` etc., how can the client of - say - `handlePerson1` method know it has to call just this method? – Tomáš Záluský Mar 24 '23 at 16:05
  • I agree with Tomáš that the information in the question is not sufficient. However, from the very last sentence ("transform handlePerson1, 2 etc.. in one single function") and from your comments here I could imagine that you maybe simply look for the [generic method syntax](https://docs.oracle.com/javase/tutorial/java/generics/methods.html): `public void handlePerson(final String request) {PersonExample personExample = ...}`. You might run into troubles with type erasure then, but is this at least the direction you're looking for? – Sebastian Mar 25 '23 at 22:13
  • Request is a JSON String I get from AWS API. I have multiple endpoints, one for every kind of person. The problem is I want to transform the handleperson1,2,3 in just one function, where the JSON string request will be converted with jackson to a object and make all generic. Is it possible? I dont know which code you want me to show because this is something more complex and I dont know the exact code you want me to show to you – Renato Quelhas Mar 27 '23 at 15:09
  • @Sebastian That is my problem!! I have problems with the type of persons, because I need to specify the type for a lot of different methods I have. For example the jackson type to transform the JSON "request" into the person1, 2 etc. – Renato Quelhas Mar 27 '23 at 15:11
  • @RenatoQuelhas if you know the explict type when you call the function `handlePerson`, then you could give it an additional argument `Class type` and then call it like `handlePerson(request, Person1.class)` . This is a common technique to avoid type erasure. See e.g. [here](https://stackoverflow.com/a/3437930/15983216) . Would this solve your problem? – Sebastian Mar 27 '23 at 19:17
  • BTW, [this question](https://stackoverflow.com/questions/8467629/type-erasure-passing-class-in-argument#8467671) seems to me somewhat related to what you are trying to do. Maybe you can also find some useful hints there? – Sebastian Mar 27 '23 at 19:22

0 Answers0