问题描述
我对这三个术语感到非常困惑.
我的理解是:
-
在工厂模式中,没有混凝土工厂.工厂根据参数构建新对象.
-
在抽象的工厂模式中,有多个具体工厂.客户必须明确创建不同的具体工厂.
是吗?
其他区别是什么?
此外,工厂方法模式是什么?它与工厂模式相同吗?
推荐答案
四个"设计模式"的帮派;可重复使用的对象软件的元素包含两个条目,"抽象出厂"(又称"虚拟构造函数")和"工厂方法".我不知道"混凝土工厂".我听过这个词,但从未考虑过太多.
工厂方法
在"工厂方法"中,一个对象具有负责另一个对象实例化的方法.一个常见的示例是JavaScript文档对象和HtmlElement对象的创建:
var newDiv = document.createElement('div');
这不是一个很好的例子,因为工厂方法的重要组成部分是多态性.如果我可以扩展document定义另一个定义另一个createElement的类,这将是主要的工厂方法材料.
抽象工厂
抽象工厂的意思是"提供一个无需指定具体类的相关对象或依赖对象的家族的接口.
典型的直通示例是小部件工厂.早在发布GOF的那一天,跨平台GUI开发就很麻烦了,因此您可以定义抽象的小部件工厂类.
该类可以具有方法createWindow,createButton,createScrollBar等.依次将定义几个实现来产生摇摆小部件或awt或其他.然后,根据配置,将实例化不同的类.
附录 - 混凝土工厂
我认为,混凝土工厂是抽象工厂或工厂方法的任何非抽取实施.
所以,当我编写自己的document概括时,覆盖createElement,我创建的类是一个具体的工厂.
同样,WidgetFactory将是一个抽象的工厂,SwingWidgetFactory将是一个具体的工厂.
其他推荐答案
最好的学习方法是阅读. 看看这一点: http://www.dofactory.com/patterns/patterns/patterns.aspx .
工厂方法将对象的延期创建为子类.这意味着定义了基类/接口,但是客户端代码不会针对此接口创建对象.实现接口的子类可初始化对象.
摘要工厂可以在这里找到: http://www.dofactory.com/图案/tatterabtract.aspx#_self2
该网站上已经有好的信息.下一个最好的事情是检查Wiki:
http://javadesign-patterns.blogspot.com/是学习的好地方设计模式
其他推荐答案
工厂模式:返回一个家庭的对象
摘要工厂:返回一个以上家庭的对象.
因此,您可以说抽象工厂包含多个工厂
问题描述
I am really confused about these three terms.
My understanding is that:
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.
Is that right?
What are the other differences?
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.
Factory Method
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');
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.
Abstract Factory
An abstract factory is meant to "provide an interface for creating families of related or dependent objects without specifying concrete classes.
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.
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.
Addendum - Concrete Factory
I believe that a Concrete Factory is any non-abstract implementation of Abstract Factory or Factory method.
So, when I write my own generalization of document which overrides createElement, the class I create is a Concrete Factory.
Likewise, while WidgetFactory would be an Abstract Factory, SwingWidgetFactory would be a concrete factory.
其他推荐答案
The best way to learn is definately to read. Take a look at this: http://www.dofactory.com/Patterns/Patterns.aspx.
Factory method's defer creation of objects to sub classes. This means that a base class/interface is defined however client code doesn't create an object against this interface. Sub classes which implement the interface are left to initialize an object.
Abstract factories can be found here: http://www.dofactory.com/Patterns/PatternAbstract.aspx#_self2
There is already good information on that site. The next best thing is to check wiki:
http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29
http://javadesign-patterns.blogspot.com/ is a good place to learn design patterns
其他推荐答案
FACTORY PATTERN: returns objects of one family
ABSTRACT FACTORY: returns objects of more than one family.
So you may say Abstract factory contains more than one factory