抽象工厂 理解

 抽象工厂模式

 

 

抽象工厂模式(英语: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/  (网站还不错的)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值