设计模式-工厂模式(手工作坊到工业化的转变 c#实现)

        还是以快餐店做例子,简单工厂模式相当于家庭作坊,随着社会的进步,分工变细,所有快餐不再是一个师父包办,而是出现了面点师,米饭师(呵呵,不知道有没有这个,反正大家明白就行),面包师分别负责专门的食物制作。这就是工厂模式,请看下面的例子:

 

using System;

namespace FactoryPattern
{
 /// <summary>
 /// 工厂模式示例
 /// </summary>
 class FactoryPattern
 {
  public static Chef nChef,rChef,bChef;
  public static Food noodle,rice,bread;
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {

        //调用示例
       nChef=new NoodlChef();
       nChef.MakeFood();
       rChef=new RiceChef();
       rChef.MakeFood();
       bChef=new BreadChef();
       bChef.MakeFood();

       Console.ReadLine();
  }
 }
 class BadFoodException: System.Exception
 {
      public BadFoodException(string strMsg)
      {
           Console.WriteLine(strMsg);
      }
 }

//定义各种快餐
 public interface Food
 {
      //烹饪
      void Cook();
      //卖出
      void Sell();

 }
 public class Noodle:Food
 {
      public  Noodle()
      {
           Console.WriteLine("/nThe Noodle is made..");
      }
      private int price;
      public void Cook()
      {
           Console.WriteLine("/nNoodle is cooking...");
      }
      public void Sell()
      {
           Console.WriteLine("/nNoodle has been sold...");
      }
      public int Price
      {
           get{return this.price;}
           set{price=value;}
      }
 }
 public class Rice:Food
 {
      public Rice()
      {
           Console.WriteLine("/nThe Rice is made ..");
      }
          private int price;
      public void Cook()
      {
           Console.WriteLine("/nRice is cooking...");
      }
      public void Sell()
      {
           Console.WriteLine("/nRice has been sold...");
      }
      public int Price
      {
           get{return this.price;}
           set{price=value;}
      }
 }
 public class Bread:Food
 {
      public Bread()
      {
           Console.WriteLine("/nThe Bread is made....");
      }
      private int price;
      public void Cook()
      {
           Console.WriteLine("/nBread is cooking...");
      }
      public void Sell()
      {
           Console.WriteLine("/nBread has been sold...");
      }
      public int Price
      {
           get{return this.price;}
           set{price=value;}
      }
 }

//定义厨师接口,并分别定义面条师,米饭师,面包师

 public interface Chef
 {

       //做饭
      Food MakeFood();
 }
 

//面条师负责制作面条
 class NoodlChef:Chef
 {
      public Food MakeFood()
          {
               return new Noodle();
          }
     }

 //米饭师负责米饭
 class RiceChef:Chef
 {
      public Food MakeFood()
      {
           return new Rice();
      }
 }

 //面包师负责做面包
 class BreadChef:Chef
 {
      public Food MakeFood()
      {
           return new Bread();
      }
 }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值