Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to implement Abstract Factory Design Pattern in Java ?.


Definition of Abstract Factory Design Pattern :- It is most powerful, highly implemented design pattern in programming languages. It provides an interface for creating families of dependent or related objects/classes without specifying their concrete classes or implementations.

Lets look at the architecture of this design pattern in Java ... 




Now as per the architecture above lets implement a simple Abstract Factory Design Pattern (See diagram below):-




Program to demonstrate how to implement Abstract Factory Design Pattern in Java. Taking above example let us code each interfaces and classes.


1. Abstract Factory for creating factories of different bikes .


package com.hubberspot.designpatterns.abstractfactory.example;

public interface BikeFactory {

 Bike createBike();

}




2. Concrete Factory (YamahaFactory) implementing Abstract Factory .


package com.hubberspot.designpatterns.abstractfactory.example;


public class YamahaFactory implements BikeFactory{

 @Override
 public Bike createBike() {
  return new FZ();
 }


}




3. Concrete Factory (HondaFactory) implementing Abstract Factory .

package com.hubberspot.designpatterns.abstractfactory.example;


public class HondaFactory implements BikeFactory {

 @Override
 public Bike createBike() {
  return new Hunk();
 }

}




4. Abstract Product interface (Bike).


package com.hubberspot.designpatterns.abstractfactory.example;

public interface Bike {

 public void drive();

}




5. Concrete Product (Hunk) for Abstract Product (Bike).

package com.hubberspot.designpatterns.abstractfactory.example;


public class Hunk implements Bike {

 @Override
 public void drive() {

  System.out.println("Hunk is driving ...");

 }

}




6. Concrete Product (FZ) for Abstract Product (Bike).

package com.hubberspot.designpatterns.abstractfactory.example;


public class FZ implements Bike {

 @Override
 public void drive() {

  System.out.println("FZ is driving ... ");

 }

}




7. A helper class Garage which will help this factories to work at the runtime.

package com.hubberspot.designpatterns.abstractfactory.example;

public class Garage {

 public Garage(BikeFactory bikeFactory) {

  Bike bike = bikeFactory.createBike();
  bike.drive();  

 }

}




8. A test class to run the above implementation of Abstract Factory Design Pattern.

package com.hubberspot.designpatterns.abstractfactory.example;

import java.util.Scanner;

public class Test {

 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in);

  System.out.println("Enter the name of bike you want to drive ... ");

  String input = scanner.next();

  if(input.equals("FZ")) {

   new Garage(new YamahaFactory());

  } else if(input.equals("Hunk")) {

   new Garage(new HondaFactory());

  } else {
   System.out.println("Sry wrong bike name entered .... ");
  }

  System.out.println("\nEnter the name of another bike ... ");

  input = scanner.next();

  if(input.equals("FZ")) {

   new Garage(new YamahaFactory());

  } else if(input.equals("Hunk")) {

   new Garage(new HondaFactory());

  } else {
   System.out.println("Sry wrong bike name entered .... ");
  }

  System.out.println("\nEnter the name of another bike ... ");

  input = scanner.next();

  if(input.equals("FZ")) {

   new Garage(new YamahaFactory());

  } else if(input.equals("Hunk")) {

   new Garage(new HondaFactory());

  } else {
   System.out.println("Sry wrong bike name entered .... ");
  }



 }  




}




Output of the program : 


 




Video tutorial to demonstrate how to implement Abstract Factory Design Pattern in Java.



















 
© 2021 Learn Java by Examples Template by Hubberspot