I am currently writing an XML converter for a supply chain project. We use Requests and Orders.
The converter has multiple method that currently do same but are separately implements for requests and orders.
I have therefore created an abstract class to improve maintainability of the code and used a generic type:
public abstract class AbstractConverter<T extends BusinessObject> Then I have the specific implementations for the actual converters
public class OrderConverter extends AbstractConverter<Order> public class RequestConverter extends AbstractConverter<Request> As I said, I have several methods in the two specific classes that basically do the same, so I naturally want them in the abstract class. I have now added the following method to the abstract class:
protected Comment createComment(T obj) { String remark; if (obj instanceof Order) { remark = ((Order) obj).getRemark(); } else if (obj instanceof Request) { remark = ((Request) obj).getRequestRemark(); } else { throw new IllegalArgumentException("This method does not support objects of the type " + obj.getClass().toString()); } return new Comment(remark); } My question now is: is this the way to go or is there a more elegant way to use generics in this context?
I need this solved but I also want to use good style.