簡體   English   中英

C#中Factory的構造方法參數,我做對了嗎?

[英]Constructor arguments for Factory in C#, am I doing it right?

我在下面解釋了我的問題,我的問題是:1.我是否正確使用工廠模式來解決該問題2.我做對了嗎?

我有一個可以稱為事件跟蹤系統的系統,該系統在工人/管理人員的建築工地中使用,它是在ASP.NET MVC中開發的,該應用程序存儲在不同位置發生的不同類型的事件。 現在,我必須為用戶提供一種基於位置,事件類型等生成報告的方法。

這就是我在代碼中所做的方式(為了簡潔起見,在某些部分中添加了注釋而不是代碼)-

//Controller methods
public ActionResult ReportByLocation(){
    var incidents = GetDataFromRepo();
    var reportFactory = new IncidentReportFactory(incidents, "ByLocation");
    var pdfView = new ReportsGenerator(reportFactory).GetPdfView();
    return pdfView;
}

public ActionResult ReportByType(){
    var incidents = GetDataFromRepo();
    var reportFactory = new IncidentReportFactory(incidents, "ByType");
    var pdfView = new ReportsGenerator(reportFactory).GetPdfView();
    return pdfView;
}

//Concrete factory class
public class IncidentReportFactory : ReportFactory{
    public IncidentFactory(List<Incident> incidents, string reportType){
        //Initialize properties
    }

    public ConcreteReport CreateConcreteReport(){
        switch(ReportType){
            case "ByLocation": return new IncidentLocationReport(incidents);
                               break;
            case "ByType": return new IncidentTypeReport(incidents);
                           break;
        }
    }
}

//ConcreteReport class
public class IncidentLocationReport : ConcreteReport{
    public IncidentLocationReport(List<Incident> incidents){
         //Constructor which sorts, splits, etc. based on location 
         //and returns
    }
}

//Report generator class
public ReportsGenerator{
     public ReportsGenerator(ReportFactory factory){
           Factory = factory;
     }

     public PDFView GetPdfView(){
          var report = factory.CreateConcreteReport();
          var pdfView = ConstructPdfWithAllFormatting(report);
          return pdfView;
     }
}

另請注意,我是從抽象工廠和具體類繼承的,我的代碼有意義嗎? 還是我做錯了一切? 請指出正確的方向。 謝謝!

基本上你是對的。

您具有帶有metohd CreateConcreteReportIncidentReportFactory ,該類創建了ConcreteReport對象,該對象取決於reportType

我認為從抽象類繼承是不是必須的。 您的ReportFactory抽象類沒有方法,因此不需要使用它。 不是巫婆可以共享的方法時,它是低抽象的。 有接口可以做到這一點。

public interface IIncidentReportFactory
{
 public IConcreteReport CreateConcreteReport();
}

並執行:

public class IncidentReportFactory : IIncidentReportFactory
{
    public IncidentFactory(List<Incident> incidents, string reportType)
    {
        //Initialize properties
    }

    public ConcreteReport CreateConcreteReport()
    {
        switch(this.ReportType)
        {
            case "ByLocation": return new IncidentLocationReport(incidents);
                               break;
            case "ByType": return new IncidentTypeReport(incidents);
                           break;
        }

      return null //no match
    }

另外,您必須更改一些名稱:

var reportFactory = new IncidentReportFactory(incidents, "ByLocation");

reportFactory是一個非常誤解的名稱。 不是reportFactoryConcreteReport對象。

當您將抽象類更改為接口時, ReportsGenerator類應像這樣

//Report generator class
public ReportsGenerator{
     public ReportsGenerator(IConcreteReport concreteReport){
           this.concreteReport= concreteReport;
     }

     public PDFView GetPdfView(){

          var pdfView = ConstructPdfWithAllFormatting(this.concreteReport);
          return pdfView;
     }
}

使用依賴注入容器也是一個好習慣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM