原文链接:http://blog.jobbole.com/78062/
在设计模式中,简单工厂模式我们也可以理解为负责生产对象的一个类, 我们平常编程中,当使用”new”关键字创建一个对象时,此时该类就依赖与这个对象,也就是他们之间的耦合度高,当需求变化时,我们就不得不去修改此类的源码,此时我们可以运用面向对象(OO)的很重要的原则去解决这一的问题,该原则就是——封装改变,既然要封装改变,自然也就要找到改变的代码,然后把改变的代码用类来封装,这样的一种思路也就是我们简单工厂模式的实现方式了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleFactoryPattern
{
/// <summary>
/// 顾客充当客户端,负责调用简单工厂来生产对象
/// 即客户点菜,厨师(相当于简单工厂)负责烧菜(生产的对象)
/// </summary>
class Program
{
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;
}
}
}