C#中工厂的构造函数参数,我做得对吗?
我已经在下面解释了我的问题,我的问题是: 1.我用工厂模式解决这个问题是否正确 2.我做得对吗 我有一个你可以称之为事件跟踪系统的系统,由工人/经理在施工现场使用,它是在ASP.NET MVC中开发的,该应用程序存储在不同地点发生的不同类型的事件。现在,我必须为用户提供一种基于位置、事件类型等生成报告的方法 我在代码中就是这样做的(为了简洁起见,在某些部分中包含了注释而不是代码)——C#中工厂的构造函数参数,我做得对吗?,c#,asp.net-mvc,design-patterns,factory,C#,Asp.net Mvc,Design Patterns,Factory,我已经在下面解释了我的问题,我的问题是: 1.我用工厂模式解决这个问题是否正确 2.我做得对吗 我有一个你可以称之为事件跟踪系统的系统,由工人/经理在施工现场使用,它是在ASP.NET MVC中开发的,该应用程序存储在不同地点发生的不同类型的事件。现在,我必须为用户提供一种基于位置、事件类型等生成报告的方法 我在代码中就是这样做的(为了简洁起见,在某些部分中包含了注释而不是代码)—— //控制器方法 公共行动结果报告ByLocation(){ var events=GetDataFromRepo
//控制器方法
公共行动结果报告ByLocation(){
var events=GetDataFromRepo();
var reportFactory=新事故报告工厂(事故,“ByLocation”);
var pdfView=newreportsgenerator(reportFactory).GetPdfView();
返回pdfView;
}
public ActionResult ReportByType(){
var events=GetDataFromRepo();
var reportFactory=新的IncidentReportFactory(事件,“按类型”);
var pdfView=newreportsgenerator(reportFactory).GetPdfView();
返回pdfView;
}
//混凝土工厂级
公共类IncidentReportFactory:ReportFactory{
公共事件工厂(列出事件,字符串报告类型){
//初始化属性
}
public ConcreteReport CreateCcreteReport(){
开关(报告类型){
案例“ByLocation”:返回新的事故地点报告(事故);
打破
案例“按类型”:返回新的事件类型报告(事件);
打破
}
}
}
//具体报告类
公共类事故地点报告:ConcreteReport{
公共事故地点报告(列出事故){
//基于位置进行排序、拆分等的构造函数
//返回
}
}
//报表生成器类
公共报告生成器{
公共报表生成器(报表工厂){
工厂=工厂;
}
公共PDFView GetPdfView(){
var report=factory.CreateConcreteReport();
var pdfView=ConstructPdfWithAllFormatting(报告);
返回pdfView;
}
}
还要注意,我是从抽象工厂和具体类继承的
我的代码有意义吗?还是我做错了?请给我指一下正确的方向。谢谢 基本上你是对的 使用metohd
CreateConcreateReport
可以创建类IncidentReportFactory
,该类创建了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
}
reportFactory
是一个非常容易误解的名称。不是reportFactory
是ConcreteReport
对象
当您将抽象类更改为接口时,ReportsGenerator
class应该这样
//Report generator class
public ReportsGenerator{
public ReportsGenerator(IConcreteReport concreteReport){
this.concreteReport= concreteReport;
}
public PDFView GetPdfView(){
var pdfView = ConstructPdfWithAllFormatting(this.concreteReport);
return pdfView;
}
}
这也是一个很好的使用方法,基本上你是对的 使用metohd
CreateConcreateReport
可以创建类IncidentReportFactory
,该类创建了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
}
reportFactory
是一个非常容易误解的名称。不是reportFactory
是ConcreteReport
对象
当您将抽象类更改为接口时,ReportsGenerator
class应该这样
//Report generator class
public ReportsGenerator{
public ReportsGenerator(IConcreteReport concreteReport){
this.concreteReport= concreteReport;
}
public PDFView GetPdfView(){
var pdfView = ConstructPdfWithAllFormatting(this.concreteReport);
return pdfView;
}
}
这也是一个很好的练习