简单工厂模式

 1 public abstract class Food
 2 {
 3     public abstract void Print();
 4 }
 5  
 6 public  class MeatFood :Food
 7 {
 8     public override void Print()
 9     {
10         Console.WriteLine("Meat food");
11     }
12 }
13  
14 public class VegetableFood : Food
15 {
16     public override void Print()
17     {
18         Console.WriteLine("Vegetable food");
19     }
20 }
21  
22 public class FoodFactory
23 {
24     public static Food getFood(string str)
25     {
26         if (str == "Meat")
27             return new MeatFood();
28         else if (str == "Vegetable")
29             return new VegetableFood();
30         else
31             return null;
32     }
33 }
34  
35 static void Main(string[] args)
36 {
37     Food food = FoodFactory.getFood("Meat");
38     food.Print();
39     food = FoodFactory.getFood("Vegetable");
40     food.Print();
41     Console.ReadLine();
42 }

 

 

posted @ 2015-12-16 11:18  ~huiChen~  阅读(145)  评论(0)    收藏  举报