Tag Archives: Abstract Factory Pattern

Design Pattern – Abstract Factory Pattern – Part IV

In the continuation of design pattern series, we have covered IntroductionSingleton Pattern and Factory Pattern. In current post, we will cover the next creational type of design pattern and that is Abstract Design Pattern.

So what is an abstract factory pattern?

It’s an interface to create families of related or dependent objects without client knowing the implementation details.

Difference between abstract factory pattern and factory pattern

Firstly, one common theme between these two patterns is that they decouple the client system from implementation details.

  • Factory pattern creates object through inheritance.
  • Abstract factory pattern creates object through composition.
  • Abstract factory provides an interface to create family of related objects.
  • Factory pattern aka factory method pattern is inherited in subclasses to create concrete objects.

In previous post, we saw an interface vehicle, implemented by different classes Car, Bus, Motorcycle, Truck and a class VehicleFactory returned different classes.

Likewise, abstract Factory pattern gives a layer of abstraction over regular factory pattern. In the process it isolates factories. The pattern returns factories of related objects. So If I want a car of Toyoto types, it can return me factories of Camry, Corolla, Avalanche etc.

Therefore, to show Abstract Factory pattern, we will create an abstract class which will return car mileage. This will look like below:

public abstract class CarMileage 
{
   protected double distance;
   protected double gas;

   public abstract void getCarMileage(double dist, double gasfilled);

   public void calculateCarMileage(double dist, double gasfilled) 
   {
     double carmileage = dist / gasfilled;
     System.out.println(" Your car mileage is " + carmileage);
   }
}

 

Now each related concrete class will extend this class to return us car mileage. We will have Camry, Corolla and Avalanche as different car of types Toyota. However, as part of this pattern, we will add an abstract class that will return me factories of car mileage. This is shown as below:

public abstract class AbstractFactory 
{
   public abstract CarMileage getCarMileage(String car);
}

 

A concrete subclass CarFactory of AbstractFactory, will return us different car mileage based on the car name we have passed.

public class CarFactory extends AbstractFactory 
{

  @Override
  public CarMileage getCarMileage(String car) 
  {

     if (car == null || car.equals("")) 
     {
       return null;
     } 
     if (car.equalsIgnoreCase("camry")) 
     {
       return new CamryCarMileage();
     } 
     else if (car.equalsIgnoreCase("corolla")) 
     {
       return new CorollaCarMileage();
     } 
     else if (car.equalsIgnoreCase("avalanche")) 
     {
       return new AvalanceCarMileage();
     }
     return null;
  }

}

 

To demo this by implementing a client class which will ask user input for carname, distance covered and gas filled. Based on carname, AbstractFactory will return us a factory for CarMileage. This factory of CarMileage will return us calculated car mileage for that car.

public class AbstractFactoryPatternDemo 
{
   public static void main(String[] args) throws IOException 
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println(" Enter the car name you want mileage for: ");
      String carname = br.readLine();
      System.out.println("\n");
      System.out.println("Enter the distance covered: ");

      String distanceStr = br.readLine();
      double distance = Double.parseDouble(distanceStr);
      System.out.println("\n");
      System.out.println("Enter the gas you had filled: ");
      System.out.println("\n");
      String gasStr = br.readLine();
      double gas = Double.parseDouble(gasStr);

      AbstractFactory af = FactoryCreator.getFactory();
      CarMileage cm = af.getCarMileage(carname);

      cm.getCarMileage(distance, gas);

   }

}

 

In conclusion, we showed how to use abstract factory patterns. If we want to create a family of related objects but without specifying their concrete sub-classes, this is our go-to pattern.

Download

The code for this post is available github repo.