Abstract Factory Pattern is a sub type of creational pattern and is the next level to factory pattern. Abstract factory pattern is known as "factory of factories".
Description
In Abstract Factory Pattern, the created interface acts as a super class, which creates a factory of related objects without specifying the class. When an independent system to be created is not associated with creation and representation of the object, then abstract design pattern is used.
When an independent system needs multiple objects, then abstract factory pattern is used. In this case, the implementation details are hidden from the clients.
Advantages
The system configuration can be done with multiple objects of a family.
Family of objects can be used together.
Design reveals the interface and library of objects.
Consistency between the objects can be achieved easily.
A framework can be designed using the abstract factory pattern.
Conceptual
figure
AbstractFactory: It is used to declare an interface for operations that create abstract products.
ConcreteFactory: It can implement operations to create concrete products.
AbstractProduct: It declares an interface for a specific type of product objects.
Product: It defines a product to be created by the corresponding ConcreteFactory. It implements the AbstractProduct interface.
Client: Ituses the interfaces declared by the AbstractFactory and AbstractProduct classes.
Examples
[java]
abstract class AbstractSystemA
{
public abstract void operationA1();
public abstract void operationA2();
}
class ProductA1 extends AbstractSystemA
{
SystemA1(String arg)
{
System.out.println("Hello "+arg);
}
//overrides the class methods
public void operationA1() { };
public void operationA2() { };
}
class ProductA2 extends AbstractSystemA
{
ProductA2(String arg)
{
System.out.println("Hello "+arg);
}
//overrides the class methods
public void operationA1() { };
public void operationA2() { };
}
abstract class AbstractProductB
{
public abstract void operationB1();
public abstract void operationB2();
}
class ProductB1 extends AbstractProductB
{
ProductB1(String arg)
{
System.out.println("Hello "+arg);
}
class ProductB2 extends AbstractProductB
{
ProductB2(String arg)
{
System.out.println("Hello "+arg);
}
}
abstract class AbstractFactory
{
abstract AbstractSystemA createSystemA();
abstract AbstractProductB createProductB();
}
class ConcreteFactory1 extends AbstractFactory
{
AbstractSystemA createSystemA()
{
return new ProductA1("ProductA1");
}
AbstractProductB createProductB()
{
return new ProductB1("ProductB1");
}
}
class ConcreteFactory2 extends AbstractFactory
{
AbstractSystemA createSystemA()
{
return new ProductA2("ProductA2");
}
AbstractProductB createProductB()
{
return new ProductB2("ProductB2");
}
}
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;
}
}
public class Client
{
public static void main(String args[])
{
AbstractFactory pf=FactoryMaker.getFactory("a");
AbstractSystemA product=pf.createSystemA();
}
}
[/java]
The AbstractFactory class contains methods for creating a new entry in the information manager. AbstractProduct classes will define methods like operationA1(), operationA2(), operationB1(), and operationB2(). ConcreteFactory and ConcreteProduct classes will implement the interfaces.
Summary
Key Points
Conditional logics can be avoided and abstract factory patterns are robust.
An interface can be created for factory without implementation. Abstract Factory Patterns can be used in JDK(Java Development Kit).