{
public class Program
{
/// <summary>
/// 程序主入口.
/// </summary>
/// <param name="s"></param>
static void Main(string[] s)
{
BreadFactory chinaFactory = new ChinaFactory();
chinaFactory.OrderBread(BreadType.cheese);
BreadFactory thailandFactory = new ThailandFactory();
thailandFactory.OrderBread(BreadType.cheese);
thailandFactory.OrderBread(BreadType.veggie);
Console.Read();
}
}
/// <summary>
/// 面包种类
/// </summary>
public enum BreadType
{
cheese,
veggie
}
/// <summary>
/// BreadStor 通过 SimpleBreadFactory 获得面包的实例
/// </summary>
public abstract class BreadFactory {
public abstract Bread CreateBread(BreadType BreadType);
public void OrderBread(BreadType BreadType){
//使用简单传入订单类型来使用工厂创建的面包
Bread Bread = CreateBread(BreadType);
#region "这是无需改变的地方 这是每个面包都需要发生的动作.."
Bread.Prepare();
Bread.Bake();
Bread.Cut();
Bread.Box();
#endregion
}
}
/// <summary>
/// 中国工厂创建的面包 应用中唯一用到具体面包类的地方
/// </summary>
public class ChinaFactory : BreadFactory
{
/// <summary>
/// 构建具体面包实例的地方 随着时间过去 面包的种类或者菜单改变 这里就必须一改再改.
/// </summary>
public override Bread CreateBread(BreadType BreadType)
{
Bread Bread = null;
if (BreadType.Equals(BreadType.cheese))
{
Bread = new ChinaCheeseBread();
}
else if (BreadType.Equals(BreadType.veggie))
{
Bread = new ChinaVeggieBread();
}
else
{
throw new Exception("no have Bread..");
}
return Bread;
}
}
/// <summary>
/// 泰国工厂创建的面包 应用中唯一用到具体面包类的地方
/// </summary>
public class ThailandFactory : BreadFactory
{
/// <summary>
/// 构建具体面包实例的地方 随着时间过去 面包的种类或者菜单改变 这里就必须一改再改.
/// </summary>
public override Bread CreateBread(BreadType BreadType)
{
Bread Bread = null;
if (BreadType.Equals(BreadType.cheese))
{
Bread = new ThailandCheeseBread();
}
else if (BreadType.Equals(BreadType.veggie))
{
Bread = new ThailandVeggieBread();
}
else
{
throw new Exception("no have Bread..");
}
return Bread;
}
}
/// <summary>
/// 面包抽象类
/// </summary>
public abstract class Bread
{
public void Prepare() { Console.WriteLine("准备.."); }
public void Bake() { Console.WriteLine("烘烤.."); }
public void Cut() { Console.WriteLine("剪切.."); }
public void Box() { Console.WriteLine("包装.."); }
}
"#region "具体产品 每个产品都会实现Bread接口
/// <summary>
/// 奶香面包
/// </summary>
public class ChinaCheeseBread : Bread
{
public ChinaCheeseBread()
{
Console.WriteLine("中国奶香面包");
}
}
/// <summary>
/// 奶香面包
/// </summary>
public class ThailandCheeseBread : Bread
{
public ThailandCheeseBread()
{
Console.WriteLine("泰国奶香面包");
}
}
/// <summary>
/// 素食面包
/// </summary>
public class ThailandVeggieBread : Bread
{
public ThailandVeggieBread()
{
Console.WriteLine("泰国素食面包");
}
}
/// <summary>
/// 素食面包
/// </summary>
public class ChinaVeggieBread : Bread
{
public ChinaVeggieBread()
{
Console.WriteLine("中国素食面包");
}
}
#endregion
}