简单工厂模式我们也可以理解为负责生产对象的一个类, 我们平常编程中,当使用"new"关键字创建一个对象时,此时该类就依赖与这个对象,也就是他们之间的耦合度高,当需求变化时,我们就不得不去修改此类的源码,此时我们可以运用面向对象(OO)的很重要的原则去解决这一的问题,该原则就是——封装改变,既然要封装改变,自然也就要找到改变的代码,然后把改变的代码用类来封装,这样的一种思路也就是我们简单工厂模式的实现方式了
namespace SimpleFactory { /// <summary> /// 顾客充当客户端,负责调用简单工厂来生产对象 /// 即客户点菜,厨师(相当于简单工厂)负责烧菜(生产的对象) /// </summary> class Customer { static void Main(string[] args) { // 客户想点一个西红柿炒蛋 Food food1 = FoodSimpleFactory.CreateFood("西红柿炒蛋"); food1.Print(); // 客户想点一个土豆肉丝 Food food2 = FoodSimpleFactory.CreateFood("土豆肉丝"); food2.Print(); Console.Read(); } } /// <summary> /// 菜抽象类 /// </summary> public abstract class Food { // 输出点了什么菜 public abstract void Print(); } /// <summary> /// 西红柿炒鸡蛋这道菜 /// </summary> public class TomatoScrambledEggs : Food { public override void Print() { Console.WriteLine("一份西红柿炒蛋!"); } } /// <summary> /// 土豆肉丝这道菜 /// </summary> public class ShreddedPorkWithPotatoes : Food { public override void Print() { Console.WriteLine("一份土豆肉丝"); } } /// <summary> /// 简单工厂类, 负责 炒菜 /// </summary> public class FoodSimpleFactory { public static Food CreateFood(string type) { Food food = null; if (type.Equals("土豆肉丝")) { food = new ShreddedPorkWithPotatoes(); } else if (type.Equals("西红柿炒蛋")) { food = new TomatoScrambledEggs(); } return food; } } }
实例:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Configuration; namespace SimpleFactory { /// <summary> /// 抽象图表接口,充当抽象类 /// </summary> internal interface Chart { void Display(); } /// <summary> /// 柱状图 /// </summary> class HistogramChart : Chart { public HistogramChart() { Console.WriteLine("创建柱状图"); } public void Display() { Console.WriteLine("显示柱状图"); } } /// <summary> /// 饼状图 /// </summary> class PieChart : Chart { public PieChart() { Console.WriteLine("创建饼状图"); } public void Display() { Console.WriteLine("显示饼状图"); } } /// <summary> /// 折线图 /// </summary> internal class LineChart:Chart { public LineChart() { Console.WriteLine("创建折线图"); } public void Display() { Console.WriteLine("显示折线图"); } } /// <summary> /// 图表工厂 /// </summary> internal class ChartFactory { public static Chart Getchart(string type) { Chart chart = null; if (type.Equals("histogram")) { chart=new HistogramChart(); Console.WriteLine("初始化设置柱状图"); } else if (type.Equals("pie")) { chart = new PieChart(); Console.WriteLine("初始化设置饼状图"); } else if (type.Equals("line")) { chart = new LineChart(); Console.WriteLine("初始化设置折现图"); } return chart; } } /// <summary> /// 客户端测试类 /// </summary> class Programs { static void Main(string[] args) { Chart chart; //读取配置文件(修改配置文件可实现创建不同的方法) string chartStr = ConfigurationSettings.AppSettings["chartType"]; chart = ChartFactory.Getchart(chartStr); chart.Display(); Console.ReadKey(); } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="chartType" value="histogram"/> </appSettings> </configuration>