What is the basic difference between the Factory and Abstract Factory Patterns?
工厂和抽象工厂模式之间的基本区别是什么?
With the Factory pattern, you produce implementations ( Apple
, Banana
, Cherry
, etc.) of a particular interface -- say, IFruit
.
With the Abstract Factory pattern, you produce implementations of a particular Factory interface -- eg, IFruitFactory
. Each of those knows how to create different kinds of fruit.
Source for this information taken from: http://java.dzone.com/news/intro-design-patterns-abstract
Abstract Factory vs. Factory Method
The methods of an Abstract Factory are implemented as Factory Methods. Both the Abstract Factory Pattern and the Factory Method Pattern decouples the client system from the actual implementation classes through the abstract types and factories. The Factory Method creates objects through inheritance where the Abstract Factory creates objects through composition.
The Abstract Factory Pattern consists of an AbstractFactory, ConcreteFactory, AbstractProduct, ConcreteProduct and Client.
How to implement
The Abstract Factory Pattern can be implemented using the Factory Method Pattern, Prototype Pattern or the Singleton Pattern. The ConcreteFactory object can be implemented as a Singleton as only one instance of the ConcreteFactory object is needed.
Factory Method pattern is a simplified version of Abstract Factory pattern. Factory Method pattern is responsible of creating products that belong to one family, while Abstract Factory pattern deals with multiple families of products.
Factory Method uses interfaces and abstract classes to decouple the client from the generator class and the resulting products. Abstract Factory has a generator that is a container for several factory methods, along with interfaces decoupling the client from the generator and the products.
When to Use the Factory Method Pattern
Use the Factory Method pattern when there is a need to decouple a client from a particular product that it uses. Use the Factory Method to relieve a client of responsibility for creating and configuring instances of a product.
When to Use the Abstract Factory Pattern
Use the Abstract Factory pattern when clients must be decoupled from product classes. Especially useful for program configuration and modification. The Abstract Factory pattern can also enforce constraints about which classes must be used with others. It may be a lot of work to make new concrete factories.
Examples:
Abstract Factory Example 1
This specification for the disks to prepare different types of pasta in a pasta maker is the Abstract Factory, and each specific disk is a Factory. all Factories (pasta maker disks) inherit their properties from the abstract Factory. Each individual disk contains the information of how to create the pasta, and the pasta maker does not.
Abstract Factory Example 2:
The Stamping Equipment corresponds to the Abstract Factory, as it is an interface for operations that create abstract product objects. The dies correspond to the Concrete Factory, as they create a concrete product. Each part category (Hood, Door, etc.) corresponds to the abstract product. Specific parts (ie, driver side door for 99 camry) corresponds to the concrete products.
Factory Method Example:
The toy company corresponds to the Creator, since it may use the factory to create product objects. The division of the toy company that manufactures a specific type of toy (horse or car) corresponds to the ConcreteCreator.
Factory pattern: The factory produces IProduct-implementations
Abstract Factory Pattern: A factory-factory produces IFactories, which in turn produces IProducts :)
[Update according to the comments]
What I wrote earlier is not correct according to Wikipedia at least. An abstract factory is simply a factory interface. With it, you can switch your factories at runtime, to allow different factories in different contexts. Examples could be different factories for different OS'es, SQL providers, middleware-drivers etc..
上一篇: Android上的MVC模式
下一篇: 工厂和抽象工厂模式之间的基本区别是什么?