Little Java design question here (for what matters it is in a context of a JEE Web Application).
Let's say I have a REST API with two operations : GET
and POST
on the same ressources. From there, using Jackson two classes are constructed representing the request's input fields. Those classes are different because the requests' parameters vary a little bit.
Let's say those two classes are named GetRequest and PostRequest.
Those two classes contains a set of fields that are common. And a set of fields that belong to each class. For instance :
public class GetRequest {
// common fields
private String callerId;
private String userId;
// non-common fields
private boolean withLinkedServices;
// Constructors, egals, hasCode, toString, getters,setter etc...
}
And for class PostRequest
public class PostRequest {
// common fields
private String callerId;
private String userId;
// non-common fields
private List<ServicesBean> services;
// Constructors, egals, hasCode, toString, getters,setter etc...
}
In the business layer of my application, I have to code an helper method (for each REST operations) which will fill another bean using common fields of each objects. The implementation of this method is exactly the same for the GET and POST operations.
The only thing that vary is that in the case of the GET
operations I have to pass the GetRequest
class and for the POST I have to pass the PostRequest
.
So my question is :
Should I work on the data model and use inheritance or should I use Generics in my helper method ? Which one would make more sense and be more efficient and resilient to application's future evolution (in case more operations are added on that ressources for instance) ?
The signature of my methods is (for the helper of the POST) :
public IDaoRequestBean buildDaoRequest(final PostRequest request);
And for the helper of the GET :
public IDaoRequestBean buildDaoRequest(final GetRequest request);