views:

2620

answers:

5

What is the basic difference between Factory and Abstract Factory Patterns?

+3  A: 

Abstract factory produces a family of objects. In .NET the perfect example of this is DbProviderFactory. David Hayden has blogged about this.

RichardOD
+5  A: 

The Abstract Factory Pattern

  • Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • The Abstract Factory pattern is very similar to the Factory Method pattern. One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.
  • Actually, the delegated object frequently uses factory methods to perform the instantiation!

Factory pattern

  • Factory patterns are examples of creational patterns

  • Creational patterns abstract the object instantiation process. They hide how objects are created and help make the overall system independent of how its objects are created and composed.

  • Class creational patterns focus on the use of inheritance to decide the object to be instantiated Factory Method

  • Object creational patterns focus on the delegation of the instantiation to another object Abstract Factory

Reference: Factory vs Abstract Factory

Syed Tayyab Ali
+10  A: 

Factory pattern: The factory produces IProduct-implementations

Abstract Factory Pattern: A factory-factory produces IFactories, which in turn produces IProducts :)

cwap
+3  A: 

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
*In the voice of Dr. Evil* Your lips are moving, but are you saying anything... no.
ceretullis
so true...this answer shouldn't be the correct answer..:/
never_had_a_name
+4  A: 

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.

How to make 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.

Abstract Factory Example1: 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 Example2: 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. Consequences: Creating objects with an Injection Mold is much more flexible than using equipment that only created toy horses. If toy unicorns become more popular than toy horses, the Injection Mold can be extended to make unicorns.

Sudhakar Kalmari
Thanks for the explaining Abstract Factory and Factory Method. I didn't understand where we use composition in abstract factory for creation of objects and where we use inheritance in factory method. It will be very useful if you post some code to explain these. Thank you very much. waiting for your code. Thanks again.
Harsha