Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
What is the difference between Factory and Abstract Factory Patterns
What is the difference between Factory and Abstract Factory Patterns?
✍: Guest
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 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 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, 5416👍, 0💬
Popular Posts:
How can you determine the size of an allocated portion of memory? You can't, really. free() can , bu...
An application needs to load a library before it starts to run, how to code? One option is to use a ...
Why is it preferred to not use finalize for clean up? Problem with finalize is that garbage collecti...
How can I search for data in a linked list? Unfortunately, the only way to search a linked list is w...
How Are Vertical Margins between Two Block Elements Collapsed? - CSS Tutorials - Understanding Multi...