Design Patterns: Abstract Factory vs Factory Method
Note: Questions are at the end of the post.
I have read the other stackoverflow threads regarding Abstract Factory vs Factory Method . I understand the intent of each pattern. However, I am not clear on the definition.
Factory Method defines an interface for creating an object, but lets subclasses decide which of those to instantiate. A factory method lets classes defer instantiation to subclasses.
By contrast, an Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
-John Feminella
The Abstract Factory looks very similar to the Factory Method . I have drawn a few UML classes to illustrate my point.
Note:
Factory Method:
Abstract Factory (only 1 member):
Abstract Factory (more members):
Questions:
Hope this helps. It describes the various types of factories. I used Head First Design Patterns as my reference. I used yuml.me to diagram.
Static Factory
Is a class with a Static Method to product various sub types of Product.
Simple Factory
Is a class that can produce various sub types of Product. (It is better than the Static Factory. When new types are added the base Product class does not need to be changed only the Simple Factory Class)
Factory Method
Contains one method to produce one type of product related to its type. (It is better than a Simple Factory because the type is deferred to a sub-class.)
Abstract Factory
Produces a Family of Types that are related. It is noticeably different than a Factory Method as it has more than one method of types it produces. (This is complicated refer to next diagram for better real-life example).
Example From The .NET Framework
DbFactoriesProvider is a Simple Factory as it has no sub-types. The DbFactoryProvider is an abstract factory as it can create various related database objects such as connection and command objects.
The two patterns are certainly related!
The difference between patterns is generally in intent.
The intent of Factory Method is "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses."
The intent of Abstract Factory is "Provide an interface for creating families of related or dependent objects without specifying their concrete classes."
Based purely on these intent statements (quoted from GoF), I would say that indeed Factory Method is in some sense a "degenerate" Abstract Factory with a family of one.
They generally tend to differ in implementation, as Factory Method is a good deal simpler than Abstract Factory .
They are related also in implementation however. As noted in the GoF book,
AbstractFactory only declares an interface for creating products. It's up to ConcreteProduct subclasses to actually create them. The most common way to do this is to define a factory method for each product.
This c2 wiki also has some interesting discussion on this topic.
It seems that the OP's list of (excellent) questions has been ignored. Current answers merely offer rehashed definitions. So I will attempt to address the original questions concisely.
No. An Abstract Factory must create more than one product to make a "family of related products". The canonical GoF example creates ScrollBar()
and Window()
. The advantage (and purpose) is that the Abstract Factory can enforce a common theme across its multiple products.
First, we must note that neither Java nor C# existed when the GoF wrote their book. The GoF use of the term interface is unrelated to the interface types introduced by particular languages. Therefore, the concrete creator can be created from any API. The important point in the pattern is that the API consumes its own Factory Method, so an interface with only one method cannot be a Factory Method any more than it can be an Abstract Factory.
This question is no longer valid, following the answers above; however, if you are left thinking that the only difference between Abstract Factory and Factory Method is the number of products created, consider how a client consumes each of these patterns. An Abstract Factory is typically injected into its client and invoked via composition/delegation. A Factory Method must be inherited. So it all comes back to the old composition vs. inheritance debate.
But these answers have raised a fourth question!
If the method is static, it is commonly called a Static Factory . If the method is non-static, it is commonly called a Simple Factory . Neither of these is a GoF pattern, but in practice they are far more commonly used!
链接地址: http://www.djcxy.com/p/50122.html上一篇: 抽象工厂与工厂方法:组合与实施?
下一篇: 设计模式:抽象工厂与工厂方法