My Java is a bit rusty, and I am sure I am doing this wrong
I have a whole bunch of different fileformats that I need to export. So I though I would create a class for each format, and have them all implement a standard Interface.
Each document has a name, and default filename, but I am not sure how I should store that data correctly. Initially I thought it should be a static final string in the concrete class, but then how do I enforce it via the interface so that all concrete classes have to supply that info.
Hope this makes sense, this is what I have so far
public interface IExporter {
public void exportToFile(String filename) throws SQLException, IOException;
public String getDocumentName();
public String getDefaultFilename();
}
–
public class LaneDrawCsvExporter implements IExporter {
public void exportToFile(String filename) throws SQLException, IOException {
// code to export document here
}
public String getDocumentName() {
return "Lane Draw";
}
public String getDefaultFilename() {
return "Lane Draw.CSV";
}
}
Your approach looks reasonable. As for how to enforce having to supply doc name and default filename, I think what you’ve done works — having methods in the interface that return the doc name and the default filename. All concrete classes implementing the interface will have to implement those methods to even be able to compile. Thus you’ve enforced the requirement that the concrete classes have to supply those names. I think trying to enforce that concrete classes have a particularly-named static final string is a red herring since you have those methods available.