Wednesday, April 20, 2011

What is difference between abstract factory and factory method.

As both belong from creational design patterns. In short they hide the
complexity of creating objects.

The main difference between factory and Abstract factory is factory method uses
inheritance to decide which object has to be instantiated. While abstract factory uses
delegation to decide instantiation of object. We can say Abstract factory uses factory
method to complete the architecture. Abstract Factory is one level higher in abstraction
over Factory.

let me give u an over view of what the actual difference is:
1.The actual product section i.e. Class “Product” it inherits from a abstract
class “AbstractProduct”.

2.The creational aspect section that’s “ConcreteCreator” class which inherits
from class “Creator”.

3.Now there are some rules the client who will need the “Product” object will
have to follow. He will never refer directly to the actual “Product” object he
will refer the “Product” object using “AbstractProduct”.

4.Second client will never use “New” keyword to create the “Product” object
but will use the “Creator” class which in turn will use the “ConcreteCreator”
class to create the actual “Product” object.

So what are benefits from this architecture?

All creational and initializing aspects are now detached from the actual client. As your creational aspect is now been handled in“ConcreteCreator” and the client has reference to only “Creator”, so any implementationchange in “CreateProduct” will not affect the client code. In short now your creational aspect of object is completely encapsulated from the client’s logic.
It creates objects for families of classes. In short it describes
collection of factor methods from various different families. In short it groups related
factory methods.
Example in this the class “Creator” is implemented using the “Abstract”
factory pattern. It now creates objects from multiple families rather one product

Compiled By Rajesh Rolen

Share This!

No comments:

Powered By Blogger · Designed By Seo Blogger Templates