Selasa, 17 Juni 2014

Difference between Factory and Abstract Factory design pattern in Java




Both Abstract Factory and Factory design pattern are creational design
pattern and use to decouple clients from creating object they need, But there
is a significant difference between Factory and Abstract Factory design pattern,
Factory
design pattern
produces implementation of
Products e.g. Garment Factory produce
different kinds of clothes, On the other hand Abstract Factory design pattern
adds another layer of abstraction
over Factory Pattern and Abstract Factory implementation itself e.g. Abstract
Factory will allow you to choose a particular Factory implementation based upon
need which will then produce different kinds of products.





In short


1) Abstract Factory design pattern  creates Factory


2) Factory design pattern creates Products






Difference between Abstract Factory and
Factory design pattern in Java



Difference between Factory vs Abstract Factory pattern in JavaLet see another example of Abstract Factory and Factory design pattern in
Java from JDK itself to get a better understanding. If you have done some XML
work in Java e.g. reading
XML files using DOM parser
, you may be familiar with
DocumentBuilderFactory  class which is an example abstract factory
design pattern because it returns a factory called
DocumentBuilder which then
used to create
Document.






//Example of Abstract Factory
and Factory design pattern  in Java


DocumentBuilderFactory abstractFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder factory = abstractFactory.newDocumentBuilder();

Document doc = factory.parse(stocks)






In this example DocumentBuilderFactory (Abstract Factory) creates DocumentBuilder (Factory)
which creates
Documents (Products).





Let's see some more difference between Abstract Factory and Factory design
pattern in Java in point form :





1) One more difference between
Abstract Factory and Factory design pattern
is that
AbstractFactory pattern
uses composition to delegate responsibility of creating object to another class
while Factory design pattern uses inheritance
and relies on derived class or sub class to create object.





2) Abstract Factory may use Factory design pattern for creating objects
but they are not just limited to that they can also use Builder
design pattern
to build object by doing series of steps or
Prototype pattern to
build object by copying or customizing prototype of that object. It completely
depends upon your implementation whether to use Factory pattern or Builder
pattern for creating products.








When
to use Abstract Factory and Factory method design pattern in Java


Factory method design pattern are modern way of creating objects. It
offers some notable advantages over
new() operator
to create Objects e.g. By using Factory method design pattern client is
completely decoupled with object creation code, which enforces Encapsulation
and result is loosely coupled and highly cohesive system. Any change e.g. a new
product from Factory requires almost no change in existing clients. See When
to use Factory method design pattern in Java
for more scenarios. On the
other hand if you need an additional level of abstraction over your Factory
pattern than Abstract Factory is the
right design pattern to use. Abstract Factory allows you to use different
Factory implementation for different purpose. Abstract Factory pattern can be
implemented using Factory method and Singleton
design pattern in Java
. One of the best
example of Abstract Factory and Factory pattern in Java
is
DocumentBuilderFactory and DocumentBuilder javax.xml.parsers package.








That's all on difference between Abstract Factory and Factory design
pattern in Java
. In short Abstract Factory design pattern provides
abstraction over Factory pattern itself while Factory design pattern provides
abstraction over products.





Other Java design pattern tutorials from Javarevisited blog





























Source:http://javarevisited.blogspot.com/2013/01/difference-between-factory-and-abstract-factory-design-pattern-java.html

Tidak ada komentar:

Posting Komentar