Java abstract factory design pattern is one level higher than the factory design pattern. It comes under the Creational Design Pattern category.
In abstract factory design pattern a super factory is responsible to create other factories of related objects. A factory will be created from a super factory class without exposing the creation logic to the client. Now, this factory will be responsible to create the objects based on Factory Design Pattern.
Main advantage of factory design pattern is it provides the loose-coupling.
Let’s look at the abstract factory design pattern with below example. We are taking here an example of two different entity types like Shape and Color. We will create two interfaces Shape and Color which will be implemented by corresponding sub classes. We will create AbstractFactory class. Next, create ShapeFactory and ColorFactory classes which will extends the AbstractFactory. A FactoryGenerator class is created to get the factory object.
ShapeFactory is responsible to create different shapes like Circle, Rectangle and Square. ColorFactory is responsible to create different colors like Green, Red, Blue.
Example
Shape.java
package com.w3schools; public interface Shape { void drawShape(); } |
Circle.java
package com.w3schools; public class Circle implements Shape { @Override public void drawShape() { System.out.println("Shape Circle."); } } |
Rectangle.java
package com.w3schools; public class Rectangle implements Shape { @Override public void drawShape() { System.out.println("Shape Rectangle."); } } |
Square.java
package com.w3schools; public class Square implements Shape { @Override public void drawShape() { System.out.println("Shape Square."); } } |
Color.java
package com.w3schools; public interface Color { void fillColor(); } |
Blue.java
package com.w3schools; public class Blue implements Color { @Override public void fillColor() { System.out.println("Fill Blue Color."); } } |
Green.java
package com.w3schools; public class Green implements Color { @Override public void fillColor() { System.out.println("Fill Green Color."); } } |
Red.java
package com.w3schools; public class Red implements Color { @Override public void fillColor() { System.out.println("Fill Red Color."); } } |
AbstractFactory.java
package com.w3schools; public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape); } |
ShapeFactory.java
package com.w3schools; public class ShapeFactory extends AbstractFactory{ //getShape method returns object of input type shape @Override public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("Circle")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("Rectangle")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("Square")){ return new Square(); } return null; } @Override Color getColor(String color) { return null; } } |
ColorFactory.java
package com.w3schools; public class ColorFactory extends AbstractFactory{ //getColor method returns object of input type color @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; } @Override public Shape getShape(String shapeType){ return null; } } |
FactoryGenerator.java
package com.w3schools; public class FactoryGenerator { public static AbstractFactory getFactory(String choice){ if(choice.equalsIgnoreCase("Shape")){ return new ShapeFactory(); }else if(choice.equalsIgnoreCase("Color")){ return new ColorFactory(); } return null; } } |
AbstractFactoryPatternTest.java
package com.w3schools; public class AbstractFactoryPatternTest { public static void main(String args[]){ //get color factory AbstractFactory shapeFactory = FactoryGenerator.getFactory("Shape"); //Get Circle class object Shape shape1 = shapeFactory.getShape("Circle"); //call drawShape method of Circle shape1.drawShape(); //Get Rectangle class object Shape shape2 = shapeFactory.getShape("Rectangle"); //call drawShape method of Rectangle shape2.drawShape(); //Get Square class object Shape shape3 = shapeFactory.getShape("Square"); //call drawShape method of Square shape3.drawShape(); //get color factory AbstractFactory colorFactory = FactoryGenerator.getFactory("Color"); //get an object of Color Red Color color1 = colorFactory.getColor("Red"); //call fillColor method of Red color1.fillColor(); //get an object of Color Green Color color2 = colorFactory.getColor("Green"); //call fillColor method of Green color2.fillColor(); //get an object of Color Blue Color color3 = colorFactory.getColor("Blue"); //call fillColor method of Color Blue color3.fillColor(); } } |
Output
Shape Circle. Shape Rectangle. Shape Square. Fill Red Color. Fill Green Color. Fill Blue Color. |