punjabtechnicaluniversity.blogspot.in
Class diagram of a factory Pattern
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 implementation change in “CreateProduct” will not affect the client code. In short now your creational aspect of object is completely encapsulated from the client’s logic.
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’s 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 below two class diagrams will provide overview of what the actual difference is. First figure shows a sample implementation of Factory Patterns. In this figure there ar etwo basic sections:-
The actual product section i.e. Class “Product” it inherits from a abstract class “AbstractProduct”.
The creational aspect section that’s “ConcreteCreator” class which inherits from class “Creator”.
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”.
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.
Class diagram of a factory Pattern
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 implementation change in “CreateProduct” will not affect the client code. In short now your creational aspect of object is completely encapsulated from the client’s logic.
Now let’s 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 relatedfactory 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..
Class Diagram of Abstract Factory
0 comments:
Post a Comment
North India Campus