What is the basic difference between the Factory and Abstract Factory Design Patterns? [closed]
Solution 1:
With the Factory pattern, you produce instances of implementations (Apple
, Banana
, Cherry
, etc.) of a particular interface -- say, IFruit
.
With the Abstract Factory pattern, you provide a way for anyone to provide their own factory. This allows your warehouse to be either an IFruitFactory
or an IJuiceFactory
, without requiring your warehouse to know anything about fruits or juices.
Solution 2:
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 for 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 (i.e., 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.
Solution 3:
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..