`

抽象工厂 理解

 
阅读更多

 抽象工厂模式

 

 

抽象工厂模式(英语:Abstract Factory)是一种常见的设计模式。
此模式为一个产品家族提供了统一的创建接口。当需要这个产品家族的某一系列的时候,
可以从抽象工厂中选出相对系的系列来创建一个具体的工厂类别。

假设我们有两种产品接口 Button 和 Border ,每一种产品都支持多种系列,
比如 Mac 系列和 Windows 系列。这样每个系列的产品分别是 MacButton, WinButton, MacBorder, WinBorder 。
为了可以在运行时刻创建一个系列的产品族,我们可以为每个系列的产品族建立一个工厂 MacFactory 和 WinFactory 。
每个工厂都有两个方法 CreateButton 和 CreateBorder 并返回对应的产品,
可以将这两个方法抽象成一个接口 AbstractFactory 。这样在运行时刻我们可以选择创建需要的产品系列。

 

其使用场景:

  • A system should be configured with one of multiple families of products
  • A system should be independent of how its products are created, composed and represented
  • Products from the same family should be used all together, products from different families ahould not be used togheter and this constraint must be ensured.
  • Only the product interfaces are revealed, the implementations remains hidden to the clients.

参考下图解释:  the same family should be used all together 指 A 、B产品要一起使用, A1 和 B1 是属于同一个family, family 其实就是 concreteFactory

 

下图 摘自 :http://www.oodesign.com/  (网站还不错的)

 

  • 大小: 23.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics