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

//Example of Abstract Factory
and Factory design pattern in Java
DocumentBuilderFactory abstractFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder factory = abstractFactory.newDocumentBuilder();
Document doc = factory.parse(stocks)
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.
Further Learning
Design Pattern Library
From 0 to 1: Design Patterns - 24 That Matter - In Java
Java Design Patterns - The Complete Masterclass
Other Java design pattern tutorials from Javarevisited blog
4 comments :
Can you please explain more with some example on the following difference you mentioned:
"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."
nice explanation.. SessionFactory is AbstractDesign pattern, and session is Factory design pattern, am i right.
I think that the approach is wrong. From what I understand, AbstractFactory is not a class that creates factories. Instead, it is a different class that creates families of related products.
With factory method, our class itself or its sublasses create the instance of the product.
With abstract factory, our class delegates the creation to another class or to subclasses of that class. This other class (or its subclasses) is the one that creates the object we need. It may also create some other related products as well (a family of products). Each product is created with its own kind of factory method.
See http://stackoverflow.com/questions/5739611/differences-between-abstract-factory-pattern-and-factory-method
@Anonymous, AbstractFactory as name state itself is to abstract Factory class, which is responsible for creating products e.g. A factory for creating Canadian Pizza could be different than factory for creating American or Italian Pizza. You can abstract this detail by using AbstractFactory pattern and depending upon whether your application is running it can pick corresponding implementation of abstract factory.
Post a Comment