I am just confused on the approach. Pls suggest me which is best. I will be creating multiple reports. SalesReport, ProfitReport etc.
Approach - 1:
class Report
{
ReportType type;
}
Subclass ReportType as SalesType, ProfitType and assign it to report instances
SalesReport:
Report sales = new Report();
sales.type = new SalesType();
ProfitReport:
Report profit = new Report();
profit.type = new ProfitType();
Approach 2:
class Report
{
}
class SalesReport : Report
{
SalesType type;
}
class ProfitReport : Report
{
ProfitType type;
}
Which approach is best? and best design? Thanks a lot.
Every report will have different criterias, different output options like Email, print etc.
class Report
{
ReportType type;
Criteria criteria;
Output output;
}
These classes are used like a Entity classes. For e.g from a browser/client we'll get the xml <Report><Type>Sales</Type><Criteria>...</Criteria></Report>. Based on the XML I need to form the Report classes and pass it for processing. Based on report type it will be executed.
If not, don't even have them inherit from a base class.
Inheritance for code reuse is an anti-pattern. If you have shared logic, consider composition.
Composition vs. Inheritance
Skip to the "When to use Inheritance and when to use Composition?" part first. It's a Java based article, but the concepts are pretty universal for static OO languages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With