【发布时间】:2016-02-23 12:18:01
【问题描述】:
我在下面解释了我的问题,我的问题是: 1.我是否正确使用工厂模式来解决这个问题 2. 我做得对吗?
我有一个你可以称之为事件跟踪系统的系统,它由工人/经理在建筑工地使用,它是在 ASP.NET MVC 中开发的,该应用程序存储在不同位置发生的不同类型的事件。现在我必须为用户提供一种根据位置、事件类型等生成报告的方法。
这就是我在代码中的做法(为简洁起见,在某些部分包含 cmets 而不是代码) -
//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;
}
}
另请注意,我是从抽象工厂和具体类继承的 我的代码有意义吗?还是我做错了?请指出我正确的方向。谢谢!
【问题讨论】:
标签: c# asp.net-mvc design-patterns factory