• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java generic template param for multiple operations  RSS feed

 
Rithanya Laxmi
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have written 2 constructors (overloaded constructors) to serve to 2 different operations as shown below:-




now I want to make it generic by using a single constructor in the BaseDetails.java which will serve any request, hence I thought of using generic parameter with "T". I can go with boolean as well but here the number of request will grow hence thought of using the generic template param.

Please let me know how we can go about it for the above code? Please clarify.

Thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rithanya Laxmi wrote:now I want to make it generic by using a single constructor in the BaseDetails.java which will serve any request, hence I thought of using generic parameter with "T". I can go with boolean as well but here the number of request will grow hence thought of using the generic template param.

I guess my question is: Why? Apart from your basic mistake (see below), what you've written seems perfectly clear to me.

Your mistake: Your ShippingDetails constructor takes a shippingRequest, but you don't say what type it is, but your BaseDetails constructor takes something called shippingDetails (again, no type). This sounds to me like a recursvie relationship rather than the usual meaning of a constructor that takes its own type, which is generally used to clone instances.

My suggestion: rather than describe to us HOW you want to do this, why don't you back up and explain the problem - ie, WHAT you want to do.

Then we'll be in a much better position to advise you.

Winston
 
Rithanya Laxmi
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston, What I want is to make the BaseDetails constructor more generic to handle any Types "T" . In the example , i have created 2 constructors in BaseDetails to serve to 2 types (shipping & order) which is working fine. Now I want to have only one BaseDetails Constructor which should serve both the shipping & order request using a generic param type. How to do that?I hope I am clear with my questions. Please clarify.

Thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rithanya Laxmi wrote:Thanks Winston, What I want is to make the BaseDetails constructor more generic to handle any Types "T" . In the example , i have created 2 constructors in BaseDetails to serve to 2 types (shipping & order) which is working fine. Now I want to have only one BaseDetails Constructor which should serve both the shipping & order request using a generic param type. How to do that?I hope I am clear with my questions. Please clarify.

OK, well I still think you're confusing two issues: generics and inheritance.

First, I suspect you have two completely separate things: a Request, and Details of an Order or Shipment generated by a request. The first is likely to be an external document; the latter an internal one, even though the contents may be very similar.

However, just going from your original code, if it was me, I think I might do it something like this:and then you might have:however, I should warn you that there are MANY ways to do this.

HIH

Winston
 
Rithanya Laxmi
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston, So in Details class how to differentiate between the requests(ShippingRequest & OrderRequest) using instanceof?

public abstract class Details<R extends Request> {
protected final R request;

public Details(R request) {
this.request = request;

if(request instanceof ShippingRequest){

// the logic for ShippingRequest

} else {

// the logic for OrderRequest

}


}

...
}

Is there any better way of doing this?
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are defeating the purpose if you need to differentiate the two classes in the parent.

This comes back to the question, why do you want to put the constructor logic of the two classes in the parent?

If there is same logic to be implemented, I would move only that portion to the parent.

If you can write the body of your classes ( even a few lines ), you will get better suggestions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!