Factroy Pattern(工厂模式)之匹萨(C#源代码)

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. namespace Factory
  7. {
  8.     class CRunMain
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             CPizzaStore objFac1 = new CNyPizzaStore();
  13.             CPizzaStore objFac2 = new CChicagoPizzaStore();
  14.             CPizza objPiz1 = objFac1.OrderPizza("cheese");
  15.             CPizza objPiz2 = objFac2.OrderPizza("cheese");
  16.             Console.WriteLine("andy love pizza: " + objPiz1.GetName());
  17.             Console.WriteLine("congfeng love pizza: " + objPiz2.GetName()); 
  18.         }
  19.     };
  20.     public abstract class CPizzaStore
  21.     {
  22.         public CPizzaStore()
  23.         {
  24.         }
  25.         public CPizza OrderPizza(string strType)
  26.         {
  27.             CPizza objPiz;
  28.             objPiz = this.CreatePizza(strType);
  29.             if (null == objPiz)
  30.             {
  31.                 return null;
  32.             }
  33.             Console.WriteLine(objPiz.Prepare());
  34.             Console.WriteLine(objPiz.Bake());
  35.             Console.WriteLine(objPiz.Cut());
  36.             Console.WriteLine(objPiz.Box());
  37.             return objPiz;
  38.         }
  39.         protected abstract CPizza CreatePizza(string strType);
  40.     };
  41.     public abstract class CPizza
  42.     {
  43.         public CPizza()
  44.         {
  45.             this.m_arrToppings = new ArrayList();
  46.         }
  47.         public string Prepare()
  48.         {
  49.             StringBuilder strbld = new StringBuilder();
  50.             strbld.Append("Prepare " + this.m_strName + "/n");
  51.             strbld.Append("Tossing " + this.m_strDough + "/n");
  52.             strbld.Append("Adding " + this.m_strSauce + "/n");
  53.             strbld.Append("Adding toppings:" + "/n");
  54.             foreach (string strTop in this.m_arrToppings)
  55.             {
  56.                 strbld.Append("/t" + strTop + "/n");
  57.             }
  58.             return strbld.ToString();
  59.         }
  60.         public virtual string Bake()
  61.         {
  62.             return "bake for 25 minutes at 350/n";
  63.         }
  64.         public virtual string Cut()
  65.         {
  66.             return "cutting the pizza into diagonal slices!/n";
  67.         }
  68.         public virtual string Box()
  69.         {
  70.             return "place pizza in official PizzaStore box!/n";
  71.         }
  72.         public string GetName()
  73.         {
  74.             return this.m_strName;
  75.         }
  76.         protected string m_strName;
  77.         protected string m_strDough;
  78.         protected string m_strSauce;
  79.         protected ArrayList m_arrToppings;
  80.     };
  81.     public class CNyStyleCheesePizza : CPizza
  82.     {
  83.         public CNyStyleCheesePizza()
  84.         {
  85.             this.m_strName = "NY Style Sauce and Cheese Pizza";
  86.             this.m_strSauce = "Martinara Sauce";
  87.             this.m_strDough = "Thin Crust Dough";
  88.             this.m_arrToppings.Add("Greated Reggiano Cheese");
  89.         }
  90.     }
  91.     public class CChicagoStyleCheesePizza : CPizza
  92.     {
  93.         public CChicagoStyleCheesePizza()
  94.         {
  95.             this.m_strDough = "Extra Thick Crust Dough";
  96.             this.m_strName = "Chicago Style Deep Dish Cheese Pizza";
  97.             this.m_strSauce = "Shredded Mozzarella Cheese";
  98.         }
  99.         public override string Cut()
  100.         {
  101.             return "Cutting the pizza into square slices /n";
  102.         }
  103.     }
  104.     public class CChicagoPizzaStore : CPizzaStore
  105.     {
  106.         public CChicagoPizzaStore()
  107.         {
  108.         }
  109.         protected override CPizza  CreatePizza(string strType)
  110.         {
  111.             if (strType.Equals("cheese"))
  112.             {
  113.                 return new CChicagoStyleCheesePizza();
  114.             }
  115.             return null;
  116.         }        
  117.     };
  118.        
  119.     public class CNyPizzaStore : CPizzaStore
  120.     {
  121.         public CNyPizzaStore()
  122.         {
  123.         }
  124.         protected override CPizza  CreatePizza(string strType)
  125.         {
  126.             if (strType.Equals("cheese"))
  127.             {
  128.                 return new CNyStyleCheesePizza();
  129.             }
  130.             return null;
  131.         }
  132.     }
  133.    
  134. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值