张德长

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

设计模式之简单工厂SimpleFactory的实现(配置文件)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="chartType" value="histogram" />
</appSettings>
</configuration>
public Chart GetChart(string type)
{
Chart chart = null;
switch (type)
{
case "histogram":
chart= new HistogramChart();
break;
case "pie":
chart =new PieChart();
break;
case "line":
chart = new LineChart();
break;
default:
break;
}
return chart;
}
}
internal interface Chart
{
void Display();
}
internal class HistogramChart:Chart
{
public HistogramChart() { Console.WriteLine("创建柱状图"); }
public void Display()
{
Console.WriteLine("显示柱状图");
}
}
internal class PieChart : Chart
{
public PieChart() { Console.WriteLine("创建饼状图"); }
public void Display()
{
Console.WriteLine("显示饼状图");
}
}
internal class LineChart : Chart
{
public LineChart() { Console.WriteLine("创建折线图"); }
public void Display()
{
Console.WriteLine("显示折线图");
}
}
internal class Program
{
static void Main(string[] args)
{
Chart chart;
SimpleFactory factory = new SimpleFactory();
string chartType = ConfigurationManager.AppSettings["chartType"];
chart = factory.GetChart(chartType);
chart.Display();
Console.Read();
}
}

posted on   张德长  阅读(57)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示