工厂、抽象工厂和工厂方法 [英] Factory, Abstract Factory and Factory Method

查看:27
本文介绍了工厂、抽象工厂和工厂方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这三个术语真的很困惑.

I am really confused about these three terms.

我的理解是:

  • 在工厂模式中,没有具体的工厂.工厂根据参数构建新对象.

  • in the Factory pattern, there is no concrete factory. The factory builds the new objects according to the parameters.

在抽象工厂模式中,有多个具体工厂.客户端必须明确地创建不同的具体工厂.

in Abstract Factory pattern, there are multiple concrete factories. The client has to create different concrete factories explicitly.

是吗?

其他区别是什么?

此外,什么是工厂方法模式?和工厂模式一样吗?

Furthermore, what is the Factory Method pattern? Is it same as the Factory pattern?

推荐答案

四人组设计模式"可重用面向对象软件的要素"本书包含两个条目,抽象工厂".(又名虚拟构造函数")和工厂方法".我不知道混凝土工厂".我听说过这个词,但从来没有想太多.

The Gang Of Four "Design Patterns; Elements of Reusable Object-Oriented Software" book contains two entries, "Abstract Factory" (aka 'Virtual Constructor') and "Factory Method". I don't know about "Concrete Factory." I've heard the term, but never given it too much thought.

在工厂方法"中一个对象有一个方法负责实例化另一个对象.一个常见的例子是 JavaScript 文档对象和 HtmlElement 对象的创建:

In "Factory Method" an object has a method which is responsible for the instantiation of another object. A common example would be the JavaScript document object and the creation of HtmlElement objects:

var newDiv = document.createElement('div');

但这不是一个很好的例子,因为工厂方法的一个重要部分是多态性.如果我可以扩展 document 来定义另一个定义另一个 createElement 的类,这将是主要的工厂方法材料.

This isn't a great example though, as an important part of the Factory Method is polymorphism. If I could extend document to define another class which defines another createElement this would be prime Factory Method material.

抽象工厂旨在提供用于创建相关或依赖对象族的接口,而无需指定具体类.

An abstract factory is meant to "provide an interface for creating families of related or dependent objects without specifying concrete classes.

典型的直接出书示例是小部件工厂;在 GoF 发布的那一天,跨平台 GUI 开发有点麻烦,所以你可以定义一个抽象的小部件工厂类.

The typical straight-out-of-the-book example is a Widget Factory; back in the day when the GoF was published, cross-platform GUI development was a bit of a hassle, so you could define an abstract widget factory class.

该类可以有方法 createWindowcreateButtoncreateScrollBar 等.反过来,将定义几个实现来生成 Swing 小部件或 AWT管他呢.然后,根据配置,将实例化不同的类.

That class could have methods createWindow, createButton, createScrollBar etc. In turn, several implementations would be defined to produce Swing widgets or AWT or whatever. Then, depending on configuration, the different class would be instantiated.

我相信具体工厂是抽象工厂或工厂方法的任何非抽象实现.

I believe that a Concrete Factory is any non-abstract implementation of Abstract Factory or Factory method.

因此,当我编写自己的覆盖 createElementdocument 概括时,我创建的类是一个具体工厂.

So, when I write my own generalization of document which overrides createElement, the class I create is a Concrete Factory.

同样,WidgetFactory 是抽象工厂,SwingWidgetFactory 是具体工厂.

Likewise, while WidgetFactory would be an Abstract Factory, SwingWidgetFactory would be a concrete factory.

这篇关于工厂、抽象工厂和工厂方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆