C # design question

I just got confused about this approach. Pls offers me which is better. I will create several reports. SalesReport, ProfitReport, etc.

Approach - 1:

class Report
{
  ReportType type;
}

Subclass ReportType as SalesType, ProfitType and assign it to instance reports

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 better design? Many thanks.

Each report will have different criteria, different output options, such as email, print, etc.

class Report
{
  ReportType type;
  Criteria criteria;
  Output output;
}

These classes are used as Entity classes. For example, from the browser / client we get xml <Report><Type>Sales</Type><Criteria>...</Criteria></Report>. Based on XML, I need to generate Report classes and pass it for processing. Based on the type of report it will be executed.

+3
source share
7

SalesReports ProfitReports ?

, .

/ ?

- -. , .

" ?" . Java-, OO.

+2

:

interface IReport {
    void RenderReport();
}

:

class SalesReport : IReport {
    void RenderReport()
    {
         // Do something here
    }
}

class ProfitReport : IReport {
    void RenderReport()
    {
         // Do something here
    }
}

:

IReport report;
report = new ProfitReport();
report.RenderReport();
report = new SalesReport();
report.RenderReport();
+1

SalesType ProfitType ReportType, , # 1.

? №2 .

0

2 , , (, ..). "SalesType", ...

" .

0

, Report, - . , - , .

, , , . SalesReport. , . . ? ? , .

0

ReportType, SalesType ProfitType? , "", - . , :

enum ReportType{
    Sales,
    Profit,
    // ...
}

2 ( ), , .

, :

void DoSomethingWithReport(Report r){
    if(r == null)
        throw new ArgumentNullException();

    switch(r.Type){
    case ReportType.Sales:
        ((SalesReport)r).DoSomething();
        break;
    case ReportType.Profit:
        ((ProfitReport)r).DoSomethingElse();
        break;            
    }
}

:

void DoSomethingWithReport(Report r){
    if(r == null)
        throw new ArgumentNullException();

    SalesReport sr = r as SalesReport;
    if(sr != null){
        sr.DoSomething();
        return;
    }

    ProfitReport pr = r as ProfitReport;
    if(pr != null);
        pr.DoSomethingElse();
}

, :

abstract class Report{
    public abstract void DoGeneralThing();
}

class SalesReport : Report{
    public override void DoGeneralThing(){
        /*Perform the old duties of SalesReport.DoSomething*/
    }
}

class ProfitReport : Report{
    public override void DoGeneralThing(){
        /*Perform the old duties of ProfitReport.DoSomethingElse*/
    }
}

void DoSomethingWithReport(Report r){
    r.DoSomething();
}
0

, 2 . , . , . , , , , , , . ProfitReports , SalesReport, SalesReport ProfitReport, .

If the two reports are very similar, then most of the code should end in the report of the base class, and derived types provide specific implementation details.

0
source

Source: https://habr.com/ru/post/1763152/


All Articles