Abstract Factory patterns works around a super-factory which creates other factories. This factory is also called as Factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
In Abstract Factory pattern an interface is responsible for creating a factory of related objects, without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.
We're going to create a Shape and Color interfaces and concrete classes implementing these interfaces. We create an abstract factory class AbstractFactory as next step. Factory classes MobileFactory and ColorFactory are defined where each factory extends AbstractFactory. A factory creator/generator class FactoryProducer is created.
AbstractFactoryPatternDemo, our demo class uses FactoryProducer to get a AbstractFactory object. It will pass information (SAMSUNG / SONY / BLACKBERRY for Mobile) to AbstractFactory to get the type of object it needs. It also passes information (RED / GREEN / BLUE for Color) to AbstractFactory to get the type of object it needs.
Step 1
Create an interface for Mobiles.
Mobile.java
public interface Mobile {
void make();
}
Step 2
Create concrete classes implementing the same interface.
Sony.java
public class Sony implements Mobile {
@Override
public void make() {
System.out.println("Inside Sony::make() method.");
}
}
Blackberry.java
public class Blackberry implements Mobile {
@Override
public void make() {
System.out.println("Inside Blackberry::make() method.");
}
}
Samsung.java
public class Samsung implements Mobile {
@Override
public void make() {
System.out.println("Inside Samsung::make() method.");
}
}
Step 3
Create an interface for Colors.
Color.java
public interface Color {
void fill();
}
Step4
Create concrete classes implementing the same interface.
Red.java
public class Red implements Color {
@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
Green.java
public class Green implements Color {
@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}
Blue.java
public class Blue implements Color {
@Override
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}
Step 5
Create an Abstract class to get factories for Color and Mobile Objects.
AbstractFactory.java
public abstract class AbstractFactory {
abstract Color getColor(String color);
abstract Mobile getMobile(String shape) ;
}
Step 6
Create Factory classes extending AbstractFactory to generate object of concrete class based on given information.
MobileFactory.java
public class MobileFactory extends AbstractFactory {
@Override
public Mobile getMobile(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("SAMSUNG")){
return new Samsung();
} else if(shapeType.equalsIgnoreCase("SONY")){
return new Sony();
} else if(shapeType.equalsIgnoreCase("BLACKBERRY")){
return new Blackberry();
}
return null;
}
@Override
Color getColor(String color) {
return null;
}
}
ColorFactory.java
public class ColorFactory extends AbstractFactory {
@Override
public Mobile getMobile(String shapeType){
return null;
}
@Override
Color getColor(String color) {
if(color == null){
return null;
}
if(color.equalsIgnoreCase("RED")){
return new Red();
} else if(color.equalsIgnoreCase("GREEN")){
return new Green();
} else if(color.equalsIgnoreCase("BLUE")){
return new Blue();
}
return null;
}
}
Step 7
Create a Factory generator/producer class to get factories by passing an information such as Mobile or Color
FactoryProducer.java
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new MobileFactory();
} else if(choice.equalsIgnoreCase("COLOR")){
return new ColorFactory();
}
return null;
}
}
Step 8
Use the FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing an information such as type.
AbstractFactoryPatternDemo.java
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
//get shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//get an object of Mobile Samsung
Mobile shape1 = shapeFactory.getMobile("SAMSUNG");
//call make method of Mobile Samsung
shape1.make();
//get an object of Mobile Sony
Mobile shape2 = shapeFactory.getMobile("SONY");
//call make method of Mobile Sony
shape2.make();
//get an object of Mobile Blackberry
Mobile shape3 = shapeFactory.getMobile("BLACKBERRY");
//call make method of Mobile Blackberry
shape3.make();
//get color factory
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
//get an object of Color Red
Color color1 = colorFactory.getColor("RED");
//call fill method of Red
color1.fill();
//get an object of Color Green
Color color2 = colorFactory.getColor("Green");
//call fill method of Green
color2.fill();
//get an object of Color Blue
Color color3 = colorFactory.getColor("BLUE");
//call fill method of Color Blue
color3.fill();
}
}
Step 9
Verify the output:
Inside Samsung::make() method.
Inside Sony::make() method.
Inside Blackberry::make() method.
Inside Red::fill() method.
Inside Green::fill() method.
Inside Blue::fill() method.