zoukankan      html  css  js  c++  java
  • 简单工厂

    using System;


    namespace SimpleFactoryPattern
    {
    /// <summary>
    /// 简单工厂模式示例
    /// </summary>
    class SimpleFactoryPattern
    {
    //定义Food接口
    public interface Food
    {
    //烹饪
    void Cook();
    //卖出
    void Sell();

    }

    //Noodle

    public class Noodle:Food
    {
    public Noodle()
    {
    Console.WriteLine("\nThe Noodle is made..");
    }
    private int price;

    //面条Noodle的Cook方法接口实现
    public void Cook()
    {
    Console.WriteLine("\nNoodle is cooking...");
    }

    //面条Noodle的Sell方法接口实现
    public void Sell()
    {
    Console.WriteLine("\nNoodle has been sold...");
    }
    public int Price
    {
    get{return this.price;}
    set{price=value;}
    }
    }

    //Rice
    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;}
    }
    }



    //Bread
    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;}
    }
    }


    //定义大厨,他包办这个快餐店里的所有Food,包括面条,面包和米饭
    class Chef
    {
    public static Food MakeFood(string foodName)
    {
    try
    {
    switch(foodName)
    {
    case "noodle": return new Noodle();
    case "rice":return new Rice();
    case "bread":return new Bread();
    default:throw new BadFoodException("Bad food request!");
    }
    }
    catch(BadFoodException e)
    {
    throw e;
    }
    }

    }

    //异常类,该餐馆没有的食品
    class BadFoodException: System.Exception
    {
    public BadFoodException(string strMsg)
    {
    Console.WriteLine(strMsg);
    }
    }


    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    Food food=Chef.MakeFood("bread");
    food.Cook();
    food.Sell();
    Console.ReadLine();
    }
    }
    }
  • 相关阅读:
    android 4.0 中出错 java.lang.UnsupportedOperationException
    怎么确定你的CPU是否支持64位虚拟化
    宽度百分比单位的转换公式
    Test SRM Level Three: LargestCircle, Brute Force
    802.11(wifi)的MAC层功能
    zookeeper集群的python代码测试
    mysqldump 命令的使用
    xp硬盘安装Fedora14 过程记录及心得体会(fedora14 live版本680M 和fedora14 DVD版本3.2G的选择)
    ContentProvider的使用
    基于 Java 2 运行时安全模型的线程协作--转
  • 原文地址:https://www.cnblogs.com/HondaHsu/p/732079.html
Copyright © 2011-2022 走看看