Pawan
Pawan

Reputation: 32331

Factory Pattern vs FactoryMethod Pattern

Could anybody please let me know why the FactoryMethod design pattern is introduced ? As my question is the same can be achivied using a Factory Pattern itself ?

For example if i see the difference between Factory Pattern and FactoryMethod Pattern the the Factory Pattern returns the Concrete IMplementation where as the FactoryMethod Pattern returns the Factory Object as its return type ??

Please tell me why FactoryMethod is introduced ??

Upvotes: 1

Views: 1354

Answers (4)

user1808932
user1808932

Reputation: 427

As per understanding about Abstract factory is it Abstract Factory pattern is used when you have factories that can create a family of objects. while there is abstraction of Factory class.

while in case of factory method have abstraction over factory class and it produces single set of product objects.

Upvotes: 0

Cratylus
Cratylus

Reputation: 54094

In a nutshell:
Factory Method is a design pattern that "hides" the instantiation of concrete types from the rest of your code thereby providing loose coupling.

Abstract Factory is a design pattern that is introduced to provide different kind of factories that are responsible to create a specific group of concrete types. So you can switch between abstract factories and as a result get eventually different concrete instances of objects in your code.

So Abstract Factory is a step up i.e. a generization of Factory Method. In simple projects the latter is adequate. In projects with complicated hierarchies the Abstract Factory is needed.

Upvotes: 1

Matei Suica
Matei Suica

Reputation: 859

AbstractFactory pattern is another level of abstractization. The user is not aware of the type of object that he will get and it's not interested in that matter. The AbstractFactory decides which method it's used to create the concrete object that the user will recieve.

In method factory, the user decides which method to use for the object creation and knows what he gets. But this doesn't always concern him.

If I request a button in Method Factory, I need to know if I want a LinuxButton or a WindowsButton or a OSXButton. In AbstractFactory, I request a button, I recieve a button... and the AbstractFactory will decide how the button is created, knowing our OS.

Upvotes: 0

Łukasz Rzeszotarski
Łukasz Rzeszotarski

Reputation: 6140

As you can read on the wiki 'Abstract factory pattern, a pattern often implemented using factory methods'.

See this picture http://upload.wikimedia.org/wikipedia/commons/a/a7/Abstract_factory.svg

Design patterns are often/ always connected with each other and this way they show us the patterns concepts.

Regards Lukasz.

Upvotes: 0

Related Questions