Crosk Cool
Crosk Cool

Reputation: 734

Best way to write pojos that can have common fields

I am currently making a service in which there are lots of public API's. And the response and request objects overlap a lot. So, I was thinking that is there a way by which we can generalise the pojo creation for the request/response objects. Sometimes the response object is identical to the request object with one or two extra fields.

Let me give you an example.

@Data
public class Request {
    private A objA;
    private B objB;
}

@Data
public class Response {
    private A objA;
    private B objB;
    private C objC;
}

@Data
public class A {
    private D objD;
}

@Data
public class B {
    private String sB; 
    private E obje;
}

@Data
public class C {
    private String sC;
}

Similary, D and E are pojos as well. The thing is that there is a lot of similarity(overlapping fields) in request/response objects.

Upvotes: 3

Views: 6874

Answers (2)

java-addict301
java-addict301

Reputation: 4136

Your solution is probably inheritance: Create a parent abstract object type with the overlapping fields and have the request and response objects extend it and specify any extra (unique) fields they need.

Inheritence

public abstract class Common {
  private String overlapfield1;
  private String overlapfield2
}

public class Request extends Common {
  private String requestField1;
  private String requestField2;
}

public class Response extends Common {
  private String responseField1;
  private String responseField2;
}

You could also approach this using composition: Create an object type with the overlapping fields and include this object as a sub-object of the Request/Response types:

Composition

public class Common {
  private String overlapfield1;
  private String overlapfield2
}

public class Request {
  private String requestField1;
  private String requestField2;
  private Common common;
}

public class Response {
  private String responseField1;
  private String responseField2;
  private Common common;
}

There are pros and cons to each approach which are widely discussed on this and other boards. These however, are the two standard approaches to dealing with such a problem.

Upvotes: 7

lance-java
lance-java

Reputation: 27996

It really depends on what you are trying to achieve. I don't see it being a huge problem repeating the fields but you've given an abstract use case rather than a real world situation where I can understand what you're trying to achieve.

Perhaps you want to pass your @Data objects to the same services? In which case you might want to use interfaces because a class can implement multiple interfaces.

Eg

public interface AContiner {
    A getA();
    void setA(A a);
}

public interface BContiner {
    B getB();
    void setB(B b);
}

@Data
public class Bean1 implements AContainer {
    private A a;
}

@Data
public class Bean2 implements AContainer, BContainer {
    private A a;
    private B b;
}

public class MyFantasticService {
    public void doStuffWithA(AContainer data) {
        System.out.println(data.getA());
    }
    public void doStuffWithB(BContainer data) {
        System.out.println(data.getB());
    }
}

Upvotes: 0

Related Questions