Difference between Abstract Factory and Factory design pattern in Java
Abstract Factory design pattern provides abstraction over
Factory pattern itself while Factory design pattern provides abstraction over
products
Abstract Factory pattern is one level higher than Factory
design patter. Abstract Factory will allow you to choose a particular Factory
implementation based upon need which will then produce different kinds of
products. Factory design pattern produces implementation of Products.
Abstract Factory may use Factory design pattern or Builder
design pattern or or Prototype pattern or
Singleton design pattern for creating objects.
Factory design pattern creates objects without exposing the
instantiation logic to the client. Refer to the newly created object through a
common interface. Abstract Factory allows you to use different Factory
implementation for different purpose for creating objects.
Factory design pattern provides flexibility hide instantiation
logic from client where an Abstract factory provides flexibility to use
different factory method for creating an object. That means one another level
of abstraction.
The client needs a product, but instead of creating it
directly using the new operator, it asks the factory object for a new product,
providing the information about the type of object it needs.
.............................................................................................................................................................
Abstract Factory pattern
It is also a
creational design pattern but it is one level higher than Factory
design patter. It is a
super-factory which creates other factories (Factory of factories).
At run time an abstract factory paired with any concrete factory to produce
object.
A concrete factory can be used Factory design pattern or Builder
design pattern or or Prototype pattern or
Singleton design pattern for creating objects.
Abstract Factory offers the interface for creating a family (factory)
of related objects, without explicitly specifying their classes.
The classic implementation for the Abstract Factory pattern
is the following:
Abstract factory with two class AbstractProductA
and AbstractProductB
abstract
class AbstractFactory{
abstract AbstractProductA
createProductA();
abstract AbstractProductB
createProductB();
}
One concrete factory implementation class
that produce product
class
ConcreteFactory1 extends AbstractFactory{
AbstractProductA
createProductA(){
return new
ProductA1("ProductA1");
}
AbstractProductB
createProductB(){
return new
ProductB1("ProductB1");
}
}
Another concrete factory implementation
class that produce product
class
ConcreteFactory2 extends AbstractFactory{
AbstractProductA
createProductA(){
return new
ProductA2("ProductA2");
}
AbstractProductB
createProductB(){
return new
ProductB2("ProductB2");
}
}
Factory creator - an indirect way of
instantiating the factories
class
FactoryMaker{
private static AbstractFactory
pf=null;
static AbstractFactory
getFactory(String choice){
if(choice.equals("a")){
pf=new
ConcreteFactory1();
}else
if(choice.equals("b")){
pf=new
ConcreteFactory2();
}
return pf;
}
}
Client
public class
Client{
public static void main(String args[]){
AbstractFactory
pf=FactoryMaker.getFactory("a");
AbstractProductA
product=pf.createProductA();
//more function
calls on product
}
}
FactoryMaker create object with the help of
ConcreteFactory1 and ConcreteFactory2 that means FactoryMaker without
specifying class can create object.
No comments:
Post a Comment