1

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 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

AMBIRD
  • 13
  • 2

2 Answers2

0

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;
    }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • This what I thought about but is it practical for storing a list that will be more than one hundred report. – AMBIRD Jul 10 '11 at 10:30
  • Hmmm, that is a lot for an enum. With *that* many you might be better to save them into a database as load them. However, the fact remains that you need to store it somewhere and it's easier to maintain the enums - just type it in. You can add and remove fields as you like, not involve DBAs with table changes, you have control. I would consider still using enums even for 100 reports. But you have to consider who will maintain the data. If users create/edit report options, use a database and web interface. Good luck. – Bohemian Jul 10 '11 at 11:11
  • I Agree with you I don't see a better/simple approach than Enums and There is no create/edit report option available for users Thanks for your time/quick answer – AMBIRD Jul 10 '11 at 11:26
0

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.

Community
  • 1
  • 1
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222