首页 > 其他 > 详细

简单工厂模式

时间:2015-12-16 12:11:30      阅读:195      评论:0      收藏:0      [点我收藏+]
 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 }

 

 

简单工厂模式

原文:http://www.cnblogs.com/foreverApril/p/5050552.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!