You can use the factory pattern.
Create an interface and define inside the base methods for A, B, C, and D.
Then all classes A, B, C, and D must implement this interface.
public interface BaseInterface {
}
Then create a factory class in which you pass the identifier and it will correctly return your reader.
package a;
public final class Factory {
private Factory() {
throw new AssertionError("Not instantiable");
}
public static CSVReader<your interface> getReader(String reader) {
if ("A".equals(reader)) {
return new CSVReader<A>();
} else if ("B".equals(reader)) {
return new CSVReader<B>();
}
}
}
Now you can call the reader through your factory class as follows:
ValueProcessorProvider provider = new ValueProcessorProvider();
CSVEntryParser<A> entryParser = new AnnotationEntryParser<A>(A.class, provider);
CSVReader<your interface> newExternalFileCSVReader =
Factory("your reader type");
List<your interface> m_NewExternalFileData = newExternalFileCSVReader.readAll();
Since you did not send classes A, B, C, and D, you need to configure this code, but after that I think you can do what you want.
source
share