工厂方法模式
工厂方法模式(Observers) 定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类的实例化推迟到子类.封装具体类型的实例化。提供创建对象的方法的接口也称为“工厂方法”。任何其他实现的方法都可能使用到这个工厂方法所制造出来的产品,但只有子类真正实现这个工厂方法并创建产品。首先完全实现‘开-闭 原则’,实现了可扩展。其次更复杂的层次结构,可以应用于产品结果复杂的场合。尽管只有一个具体创建者依然可以使用,它帮助我们将产品的“实现”从“使用”中解藕。
在本次Dome中,采用的方式是"参数化工厂方法",它可以更具传入的参数创建不同的对象,然而,工厂经常只产生一种对象,不需要参数化,模式的这两种形式都是有效的,
松耦合的设计之所以能让我们建立有弹性的OO系统,能够应对变化,是因为对象之间的互相依赖降到了最低。所以我们为了交互对象之间的松耦合设计而在努力。
Demo - 国际面包店
该面包店可以生产多种不同的面包,同样种类的面包在不同国家生产的面包口味有所不一样,那么在这样的设计中,首先工厂,是必须可以随时扩展,当然面包的种类也会越来越多.~
![](http://img1.51cto.com/attachment/200807/282288_1217490614.jpg)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
#region "这是无需改变的地方 这是每个面包都需要发生的动作.."
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
"#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
}
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)
![](http://maddish.blog.51cto.com/images/editer/InBlock.gif)