What is the difference between Factory and Abstract Factory Patterns
What is the difference between Factory and Abstract Factory Patterns?
Note: - This is quiet a confusing architect question especially in design pattern section.
Interviewer can take you for a nice ride. So get the difference in your heart.
First read the definition provided in the first question about both these patterns. The common thing they have is that they belong to creational 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.
The two class diagrams below will provide overview of what is the actual difference. First figure shows a sample implementation of Factory Patterns. In this figure there are two basic sections:-
ã The actual product section i.e. Class gProducth it inherits from an abstract class gAbstractProducth.
ã The creational aspect section i.e. gConcreteCreatorh class which inherits from class gCreatorh.
ã Now there are some rules the client will have to follow who will need the gProducth object. He will never refer directly to the actual gProducth object he will refer the gProducth object using gAbstractProducth.
ã Second client will never use gNewh keyword to create the gProducth object but will use the gCreatorh class which in turn will use the gConcreteCreatorh class to create the actual gProducth object.
So what are the 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 implementation change in CreateProduct will not affect the client code. In short now your creational aspect of object is completely encapsulated from the clients logic.
Now lets look at the second class diagram which provides an overview of what actually Abstract factory pattern is. 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.
Note :- Just stick up to this definition that Abstract factory classifies factory methods or groups logically related factory method together.
2007-10-24, 4879👍, 0💬
Can you explain project life cycle ? Figure :- 12.2 Life cycle of a project There are five stages of...
How can I execute a PHP script using command line? Just run the PHP CLI (Command Line Interface) pro...
Can you explain why your project needed XML? Remember XML was meant to exchange data between two ent...
Can we have static indexer in C# ? No.
What is the sequence of UML diagrams in project? First let me say some fact about this question, you...