For a project we have a requirement to create an interfacedefinition that will return all available filetype extensions that our component can export...
The problem is that we want avoid configuration/properties files. We don't want to edit our configuration/propertie file when another filetype is added (in the future). The structure of this part of our component is as follows:
public abstract class FileType {
protected String filetype;
public FileType(String filetype){
this.filetype = filetype;
}
public abstract void export(String path, Object information);
}
public class PdfExport extends FileType {
public PdfExport() {
super("pdf");
}
public void export(String path, Object information){
//pdf specific logic
}
}
But how do we solve this when another component calls the interfacedefinition getExportTypes()? (How do we get a list of all available filetypes?) Taking into account the requirement to add in the future new classes that extend abstract class filetype (add new filetypes)?
Does anyone has suggestions, maybe another structure of above example? Or any (design) that discuss above issue?
Thanks in advance!
This is the exact purpose of the strategy pattern. The strategies here are the
FileTypes that encapsulate an algorithm that exports a file. In the following example:The
Applicationclass holds a list of exporters that can be filled out on the go. TheApplicationclass does not have to know what type of file exporter is registered nor how the file can be exported. When the data is exported, theApplicaitonclass loops through registered exporters and delegates the export task to each one of them.EDIT Below is an example of the
Applicationclass usage.EDIT How to avoid configuration files and changing the code everytime you have a new
FileType? You can load the exporters at runtime using reflexion (see this link for details)