Created
December 5, 2018 07:07
-
-
Save danielplawgo/228d3e919ebc7ed16fa8e153f237b08e to your computer and use it in GitHub Desktop.
Jak zastąpić rozbudowanego switch w aplikacji
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CsvFormatter : ICsvFormatter | |
{ | |
public void Export(Report report) | |
{ | |
Console.WriteLine($"CsvFormatter.Export: {report.Name}"); | |
} | |
} | |
public interface ICsvFormatter | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CsvFormatter : ICsvFormatter | |
{ | |
public ExportFormat Format { get; } = ExportFormat.Csv; | |
public void Export(Report report) | |
{ | |
Console.WriteLine($"CsvFormatter.Export: {report.Name}"); | |
} | |
} | |
public interface ICsvFormatter : IFormatter | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum ExportFormat | |
{ | |
Xml, | |
Csv | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IFormatter | |
{ | |
ExportFormat Format { get; } | |
void Export(Report report); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Report | |
{ | |
public string Name { get; set; } | |
public ExportFormat ExportFormat { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReportProcessorWithoutSwitch : IReportProcessor | |
{ | |
private IEnumerable<IFormatter> _formatters; | |
public ReportProcessorWithoutSwitch(IEnumerable<IFormatter> formatters) | |
{ | |
_formatters = formatters; | |
} | |
public void Process(Report report) | |
{ | |
var formatter = _formatters.FirstOrDefault(f => f.Format == report.ExportFormat); | |
if (formatter == null) | |
{ | |
throw new Exception("Not supported report formatter."); | |
} | |
formatter.Export(report); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReportProcessorWithSwitch : IReportProcessor | |
{ | |
private IXmlFormatter _xmlFormatter; | |
private ICsvFormatter _csvFormatter; | |
public ReportProcessorWithSwitch(IXmlFormatter xmlFormatter, | |
ICsvFormatter csvFormatter) | |
{ | |
_xmlFormatter = xmlFormatter; | |
_csvFormatter = csvFormatter; | |
} | |
public void Process(Report report) | |
{ | |
switch (report.ExportFormat) | |
{ | |
case ExportFormat.Xml: | |
_xmlFormatter.Export(report); | |
break; | |
case ExportFormat.Csv: | |
_csvFormatter.Export(report); | |
break; | |
default: | |
throw new ArgumentOutOfRangeException(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class XmlFormatter : IXmlFormatter | |
{ | |
public void Export(Report report) | |
{ | |
Console.WriteLine($"XmlFormatter.Export: {report.Name}"); | |
} | |
} | |
public interface IXmlFormatter | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class XmlFormatter : IXmlFormatter | |
{ | |
public ExportFormat Format { get; } = ExportFormat.Xml; | |
public void Export(Report report) | |
{ | |
Console.WriteLine($"XmlFormatter.Export: {report.Name}"); | |
} | |
} | |
public interface IXmlFormatter : IFormatter | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment