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