关闭

再次研究设计模式 第一章 抽象工厂

标签: 设计模式产品applicationinterfaceclassuml
973人阅读 评论(0) 收藏 举报

最近苦学UML2.0,突然发现原先以为了解的设计模式已经淡忘了。最近5年来国内介绍这个的书是多如牛毛啊,不外乎互相搬砖。于是决定自己再从英文原版中翻译一下,顺便还可以加上自己的理解。

工作日每天翻译一段吧。英文原文附后,免得译得不好还要再找原文。

原文从UML设计工具 StartUML中摘抄

 

1 抽象工厂

目的

  • 为生成一组相关或者相互依赖的类提供一个接口,真正创建的产品的工作由子类实现。Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

结构

 

  • AbstractFactory 抽象工厂: 创建抽象产品的抽象的操作者。

  • ConcreteFactory (实现工厂): 具体产品类对象创建的实现者。

  • AbstractProduct : 某一类型(具有相同接口)产品类的接口声明。

  • ConcreteProduct(): 由创建的产品对象的操作接口。

  • Client : 只使用由AbstractFactoryAbstractProduct来创建实际的产品。

应用场合

  • 独立于产品如何创建、组合和表示的系统。a system should be independent of how its products are created, composed, and represented.

  • 会被几类产品中的某一类配置确定的系统。a system should be configured with one of multiple families of products.

  • 如果你要设计使用一系列互相关联的产品,用此模式来加强相互间的约束。a family of related product objects is designed to be used together, and you need to enforce this constraint.

  • 你想提供一系列的产品的产品库,只暴露产品的接口而不是它们的实现。you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

模式效果

  • 分离具体类It isolates concrete classes. 因为抽象工厂封装了实际产品对象的创建过程,它将客户与实现类分离,所以该抽象工厂模式可以帮助你操作应用程序创建的一组类。而产品类的名字被实现工厂分离,不会被客户代码知道,客户代码只需通过抽象接口操作产品实例。The Abstract Factory pattern helps you control the classes of objects that an application creates. Because a factory encapsulates the responsibility and the process of creating product objects, it isolates clients from implementation classes. Clients manipulate instances through their abstract interfaces. Product class names are isolated in the implementation of the concrete factory; they do not appear in client code.

  • 可以方便是交换产品类。It makes exchanging product families easy. 在应用程序中,一类产品的实现工厂只有一个,就是在被真正实例化的那个。这使得在应用程序中改变实现工厂非常方便—你只需更改实现工厂就可以实现不同的产品配置。由于抽象工厂生成全部的产品类,所有的产品都可以被一次性更改。The class of a concrete factory appears only once in an application—that is, where it's instantiated. This makes it easy to change the concrete factory an application uses. It can use different product configurations simply by changing the concrete factory. Because an abstract factory creates a complete family of products, the whole product family changes at once.

  • 它促进了产品的一致性It promotes consistency among products. 当有几类的产品在一起协同工作时,应用程序只在同一时间内使用一类产品就非常重要,抽象工厂模式就能很容易地保证这一点。When product objects in a family are designed to work together, it's important that an application use objects from only one family at a time. AbstractFactory makes this easy to enforce.

  • 想要支持新类型的产品很困难Supporting new kinds of products is difficult. 扩充AbstractFactory来支持新类型的产品是不方便,因为AbstractFactory的接口限定了能够生成的产品类,要想支持新的产品类就需要扩展抽象工厂,这需要扩展AbstractFactory以及它所有的子类Extending abstract factories to produce new kinds of Products isn't easy. That's because the AbstractFactory interface fixes the set of products that can be created. Supporting new kinds of products requires extending the factory interface, which involves changing the AbstractFactory class and all of its subclasses.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11498次
    • 积分:222
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类