Store Report names in an Interface , Enum , or as constant inside the report handler class?
For every Report I have a special handler class for that report currently I store the report name as a private constant inside the handler class.
I use the report name as a key to get this report specific configuration from the reports configuration file.
I thought about changing this and store 开发者_JS百科all the report names in an enum , the other approach I have in mind is to use the fully qualified name of the handler class as the key in the configuration file.
I need to know which approach is better : Interface , Enum , Handler class constant or use the class name as a key in the configuration file instead of the report name
Please Advice
I really like using enums and have used them often. You have to store the data somewhere, why not put it where everyone can find it!
Do something like this:
public enum Report {
SALES("Sale Report", "select * from some_view"),
PROFIT("Profit Report", "select * from some_other_view");
final String reportName;
final String sql;
private Report(String reportName, String sql) {
this.reportName = reportName;
this.sql = sql;
}
public String getReportName() {
return reportName;
}
public String getSql() {
return sql;
}
}
You could also use an inheritance hierarchy; see this question. That is, have a SaleReport
class that inherits from your base Report
class, and a ProfitReport
class that inherits from Report
, etc. Have a different subclass for each different report type, and a common base report class.
精彩评论