using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //如果我们需要加入什么类型的食物,添加子类就可以 FoodFactory _foodfactory = new FoodFactory(); Food _food = _foodfactory.CreateFood(2); _food.MakeFood(); } } /// <summary> /// 食物类型 /// </summary> public class Food { /// <summary> /// 生产食物 /// </summary> public virtual void MakeFood() { } } /// <summary> /// 食物工厂 /// </summary> public class FoodFactory { public Food CreateFood(int type) { Food _food; switch (type) { case 1: _food = new YaBo(); break; default: _food = new YaJia(); break; } return _food; } } /// <summary> /// 生产鸭脖 /// </summary> public class YaBo : Food { public override void MakeFood() { Console.WriteLine("鸭脖"); Console.ReadLine(); } } /// <summary> /// 生产鸭架 /// </summary> public class YaJia : Food { public override void MakeFood() { Console.WriteLine("鸭架"); Console.ReadLine(); } } }