Abstract Factory Pattern - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Sunday, July 31, 2016

Abstract Factory Pattern

The abstract factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is part of creational design pattern, it is also called as factory of factories.

Abstract factory allows a client to use an abstract interface to create a set of related products(or objects) without knowing (or caring) about the concrete products that are actually produced.

Implementation

We are going to create a Vehicle and a Color interface ,and concrete classes implementing these interfaces. We have created VehicleFactory and ColorFactory that extends AbstractFactory interface. A factory handler/creator/generator class FactoryHandler is created.

UML Diagram:

Abstract Factory Pattern UML

Step 1

Create a Vehicle interface.
public interface Vehicle {
 void printVehicle();
}

Step 2

Create concrete class extending Vehicle interface.

public class TwoWheeler implements Vehicle {

 @Override
 public void printVehicle() {
  System.out.println("I am two wheeler");
 }
}
public class ThreeWheeler implements Vehicle {

 @Override
 public void printVehicle() {
  System.out.println("I am three wheeler");
 }
}
public class FourWheeler implements Vehicle {

 @Override
 public void printVehicle() {
  System.out.println("I am four wheeler");
 }
}

Step 3

Create a Color interface.

public interface Color {
 public void paint();
}

Step 4

Create concrete classes that implements Color interface.

public class Red implements Color {

 @Override
 public void paint() {
  System.out.println("Vehicle painted with red color.");
 }
}
public class Green implements Color {

 @Override
 public void paint() {
  System.out.println("Vehicle painted with green color.");
 }
}
public class Blue implements Color {

 @Override
 public void paint() {
  System.out.println("Vehicle painted with blue color.");
 }
}

Step5

Create AbstractFactory interface to get factories for Vehicle and Color objects.

The AbstractFactory class defines the interface that all concrete factories must implement, which consist of a set of methods for producing objects.

public interface AbstractFactory {
 public Vehicle getVehicle(String vName);

 public Color getColor(String color);
}

Step 6

Create factory classes that will implement AbstractFactory interface and generate object of concrete classes base of given information.

public class VehicleFactory implements AbstractFactory {
 public Vehicle getVehicle(String vName) {
  if (vName == null)
   return null;
  else if (vName.equalsIgnoreCase("TwoWheeler"))
   return new TwoWheeler();
  else if (vName.equalsIgnoreCase("ThreeWheeler"))
   return new ThreeWheeler();
  else if (vName.equalsIgnoreCase("FourWheeler"))
   return new FourWheeler();
  else
   return null;
 }

 @Override
 public Color getColor(String color) {
  return null;
 }
}
public class ColorFactory implements AbstractFactory {

 @Override
 public Vehicle getVehicle(String vName) {
  return null;
 }

 @Override
 public Color getColor(String color) {
  if (color == null)
   return null;
  else if (color.equalsIgnoreCase("RED"))
   return new Red();
  else if (color.equalsIgnoreCase("BLUE"))
   return new Blue();
  else if (color.equalsIgnoreCase("GREEN"))
   return new Green();
  else
   return null;
 }
}

Step 7

Create a FactoryHandler class to get factory object based on some information like Vehicle or Color.

public class FactoryHandler {
 public static AbstractFactory getFactory(String choice) {
  if (choice == null)
   return null;
  else if (choice.equalsIgnoreCase("VEHICLE"))
   return new VehicleFactory();
  else if (choice.equalsIgnoreCase("COLOR"))
   return new ColorFactory();
  else
   return null;
 }
}

Step 8

Create AbstractFactoryPatternClient class that will test the abstract factory pattern.

public class AbstractFactoryPatternClient {

 public static void main(String[] args) {
  // get Vehicle factory
  AbstractFactory vehicleFactory = FactoryHandler.getFactory("VEHICLE");
  // get an object of TwoWheeler
  Vehicle twoWheeler = vehicleFactory.getVehicle("TWOWHEELER");
  // call printVehicle method of TwoVehicle
  twoWheeler.printVehicle();

  // get an object of ThreeWheeler
  Vehicle threeWheeler = vehicleFactory.getVehicle("THREEWHEELER");
  // call printVehicle method of ThreeVehicle
  threeWheeler.printVehicle();

  // get an object of FourWheeler
  Vehicle fourVehicle = vehicleFactory.getVehicle("FOURWHEELER");
  // call printVehicle method of FourVehicle
  fourVehicle.printVehicle();

  // get Color Factory
  AbstractFactory colorFactory = FactoryHandler.getFactory("COLOR");
  // get an object of color Red
  Color red = colorFactory.getColor("RED");
  // call paint method of Red
  red.paint();

  // get an object of color Green
  Color green = colorFactory.getColor("GREEN");
  // call paint method of Green
  green.paint();

  // get an object of color Blue
  Color blue = colorFactory.getColor("BLUE");
  // call paint method of Blue
  blue.paint();

 }
}

Output of the above program shown below.

I am two wheeler
I am three wheeler
I am four wheeler
Vehicle painted with red color.
Vehicle painted with green color.
Vehicle painted with blue color.
Note: Depend on abstractions, Do not depend on concrete classes.

1 comment :

  1. factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
    factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
    factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.


    ReplyDelete