Learn About Abstract Factory Design Pattern

1
1281
Abstract Factory Design Pattern

 

In the earlier chapter, we discussed about the factory pattern where an object is created without exposing the actual creation logic to the client and with the help of a common interface reference can be made to a newly created object. In this chapter, we are going to discuss about Abstract Factory patterns which behaves as a super-factory that creates a number of other factories. Therefore, such super-factory is also known as factory of factories. Like factory pattern, abstract factory pattern also comes under the category of creational design pattern that provide a common and very simple way to create objects and hide the actual creation logic.

In the Abstract Factory pattern, it uses an interface which is responsible for the creation of a factory of related objects where there is no need to explicitly specify their classes. Each factory which is generated is capable of providing the objects as per the Factory design pattern.

Implementation of Abstract Factory design pattern
To understand the abstract factory design pattern, we are going to create the Vehicle and Color interfaces and their concrete classes (Car, Bike and Truck classes) and (RedColor, GreenColor and BlueColor classes) which implement the Vehicle and Color interfaces respectively. Next, we create an abstract factory class which will be inherited by other factory classes (here VehicleFactory and ColorFactory classes). These classes extends the AbstractFactoryClass. Further, a FactoryProducerClass is created in order to get factories by passing information such as Vehicle or Color type choice.

The implementation class i.e. AbstractFactoryPatternDemo will use FactoryProducerClass to procure an AbstractFactoryClass object. It will pass information such as CAR / BIKE / TRUCK for Vehicle type to AbstractFactoryClass in order to procure the type of object it requires. Further, it will pass information such as RED / GREEN / BLUE for Color type to AbstractFactoryClass in order to procure the type of object it requires.

Let’s summarize below the step by step implementation of Abstract Factory design pattern.

Step 1: – Creation of an interface i.e. Vehicle.java

package com.eduonix.design.pattern;
/**
 * 
 * @author Aparajita
 *
 */
public interface Vehicle {
	
	void drive();
}

Step 2: – Creation of the three concrete classes (i.e., Car, Bike and Truck classes) which implement the same interface i.e. Vehicle.java
Car.java

package com.eduonix.design.pattern;
/**
 * 
 * @author Aparajita
 *
 */
public class Car implements Vehicle{
	
	@Override
	public void drive() {
		System.out.println ("Inside Car::drive () method.");
	}
}

Bike.java

package com.eduonix.design.pattern;
/**
 * 
 * @author Aparajita
 *
 */
public class Bike implements Vehicle{

	@Override
	public void drive() {
		System.out.println ("Inside Bike::drive () method.");
	}
}

Truck.java

package com.eduonix.design.pattern;
/**
 * 
 * @author Aparajita
 *
 */
public class Truck implements Vehicle{

	@Override
	public void drive() {
		System.out.println ("Inside Truck::drive () method.");
	}
}

Step 3: – Creation of an interface i.e. Color.java
Color.java

package com.eduonix.design.pattern;
/**
 * 
 * @author Aparajita
 *
 */
public interface Color {
	void fillColor();
}

Step 4: – Creation of the three concrete classes (i.e., RedColor, GreenColor and BlueColor classes) which implement the same interface i.e. Color.java
RedColor.java

package com.eduonix.design.pattern;

/**
 * 
 * @author Aparajita
 *
 */
public class RedColor implements Color {

	   @Override
	   public void fillColor() {
	      System.out.println ("Inside Red::fillColor() method.");
	   }
	}

GreenColor.java

package com.eduonix.design.pattern;

/**
 * 
 * @author Aparajita
 *
 */
public class GreenColor implements Color {

	@Override
	public void fillColor() {
		System.out.println ("Inside Green::fillColor() method.");
	}
}

BlueColor.java

package com.eduonix.design.pattern;

/**
 * 
 * @author Aparajita
 *
 */
public class BlueColor implements Color {

	@Override
	public void fillColor() {
		System.out.println ("Inside Blue::fillColor() method.");
	}
}

Step 5: – Creation of an Abstract class to get factories for Color and Vehicle Objects.
AbstractFactoryClass.java

package com.eduonix.design.pattern;

/**
 * 
 * @author Aparajita
 *
 */
public abstract class AbstractFactoryClass {
	
	abstract Color getColor(String color);

	abstract Vehicle getVehicle(String vehicle);
}

Step 6: – Creation of the Factory classes extending AbstractFactoryClass in order to generate object of concrete class based on the available information.
VehicleFactory.java

package com.eduonix.design.pattern;

/**
 * 
 * @author Aparajita
 *
 */
public class VehicleFactory extends AbstractFactoryClass{

	   @Override
	   public Vehicle getVehicle(String vehicleType){
	   
	      if(vehicleType == null){
	         return null;
	      }		
	      
	      if(vehicleType.equalsIgnoreCase("CAR")){
	         return new Car();
	         
	      }else if(vehicleType.equalsIgnoreCase("BIKE")){
	         return new Bike();
	         
	      }else if(vehicleType.equalsIgnoreCase("TRUCK")){
	         return new Truck();
	      }
	      
	      return null;
	   }
	   
	   @Override
	   Color getColor(String color) {
	      return null;
	   }
}

ColorFactory.java

package com.eduonix.design.pattern;
/**
 * 
 * @author Aparajita
 *
 */
public class ColorFactory extends AbstractFactoryClass{

	   @Override
	   public Vehicle getVehicle(String vehicleType){
	      return null;
	   }
	   
	   @Override
	   Color getColor(String color) {
	   
	      if(color == null){
	         return null;
	      }		
	      
	      if(color.equalsIgnoreCase("RED")){
	         return new RedColor();
	         
	      }else if(color.equalsIgnoreCase("GREEN")){
	         return new GreenColor();
	         
	      }else if(color.equalsIgnoreCase("BLUE")){
	         return new BlueColor();
	      }
	      
	      return null;
	   }
}

Step 7: – Creation of a Factory generator/producer class in order to get factories by passing an information such as Vehicle or Color type choice.
FactoryProducerClass.java

package com.eduonix.design.pattern;

/**
 * 
 * @author Aparajita
 *
 */
public class FactoryProducerClass {

	public static AbstractFactoryClass getFactory(String choice) {

		if (choice.equalsIgnoreCase("VEHICLE")) {
			return new VehicleFactory();

		} else if (choice.equalsIgnoreCase("COLOR")) {
			return new ColorFactory();
		}

		return null;
	}
}

Step 8: – Now, we can use the FactoryProducerClass to get AbstractFactoryClass in order to get factories of concrete classes by providing an information such as choice type (vehicle or color).
AbstractFactoryPatternDemo.java

package com.eduonix.design.pattern;

/**
 * 
 * @author Aparajita
 *
 */
public class AbstractFactoryPatternDemo {

	   public static void main(String[] args) {

		      //get shape factory
		      AbstractFactoryClass shapeFactory = FactoryProducerClass.getFactory("VEHICLE");

		      //get an object of Vehicle Car
		      Vehicle vehicle1 = shapeFactory.getVehicle("CAR");

		      //call drive method of Vehicle Car
		      vehicle1.drive();

		      //get an object of Vehicle Car
		      Vehicle vehicle2 = shapeFactory.getVehicle("BIKE");

		      //call drive method of Vehicle Bike
		      vehicle2.drive();
		      
		      //get an object of Vehicle Truck 
		      Vehicle vehicle3 = shapeFactory.getVehicle("TRUCK");

		      //call drive method of Vehicle Truck
		      vehicle3.drive();

		      //get color factory
		      AbstractFactoryClass colorFactory = FactoryProducerClass.getFactory("COLOR");

		      //get an object of Color Red
		      Color color1 = colorFactory.getColor("RED");

		      //call fill method of Red
		      color1.fillColor();

		      //get an object of Color Green
		      Color color2 = colorFactory.getColor("Green");

		      //call fill method of Green
		      color2.fillColor();

		      //get an object of Color Blue
		      Color color3 = colorFactory.getColor("BLUE");

		      //call fill method of Color Blue
		      color3.fillColor();
		   }
}

Step 9: – When we execute AbstractFactoryPatternDemo class as a Java application then will observe the following desired output which demonstrate the Abstract Factory design pattern.

Inside Car::drive () method.
Inside Bike::drive () method.
Inside Truck::drive () method.
Inside Red::fillColor () method.
Inside Green::fillColor () method.
Inside Blue::fillColor () method.

Conclusion: –
In this chapter, we discussed about the abstract factory design pattern in detail along with a suitable example. In that example we have demonstrated the step by step creation of an Interface, concrete classes, abstract factory class, factory producer class and Abstract Factory design pattern implementer class.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here