Factory methods let us to choose how our class decides the way of interface implementation.
maxresdefault
Package can have multiple implemented classes in same interface and if we need to change the implementation of interface we have to change all external classes which are using interface implemented classes.  Therefore factory method introduces a factory class that we can decide which we need to use. And then we only need to change one class if interface implementation changed.
Check below explanation.
An interface has implemented with few methods as below.

public interface FuelCalculation {
    public String getVehicleType(int category);
    public double getFuelConsumption(int rpm, String type);
}

Which calculate the fuel consumption according to vehicle type and rpm. Please ignore mechanical calculation and just take the idea because fuel consumption calculation formula can be wrong.
This is interface was implemented by two classes such as Petrol and Diesel.

class PetrolCarConsumption implements FuelCalculation{
    @Override
    public String getVehicleType(int category) {
        String carType;
        switch (category) {
            case 1:
                carType = "LIGHT_VEHICLE";
                break;
            case 2:
                carType = "HEAVY_VEHICLE";
                break;
            default:
                carType = "MIDDLE_LEVEL_VEHICLE";
        }
        return carType;
    }
    @Override
    public double getFuelConsumption(int rpm, String type) {
        double consumption = 0;
        switch (type) {
            case "LIGHT_VEHICLE":
                consumption = .10 * rpm;
                break;
            case "HEAVY_VEHICLE":
                consumption = .06 * rpm;
                break;
            case "MIDDLE_LEVEL_VEHICLE":
                consumption = .08 * rpm;
                break;
            default:
                consumption = .09 * rpm;
                break;
        }
        return consumption;
    }
}
class DieselCarConsumption implements FuelCalculation{
    @Override
    public String getVehicleType(int category) {
        String carType;
        switch (category) {
            case 1:
                carType = "LIGHT_VEHICLE";
                break;
            case 2:
                carType = "HEAVY_VEHICLE";
                break;
            default:
                carType = "MIDDLE_LEVEL_VEHICLE";
        }
        return carType;
    }
    @Override
    public double getFuelConsumption(int rpm, String type) {
        double consumption = 0;
        switch (type) {
            case "LIGHT_VEHICLE":
                consumption = .12 * rpm;
                break;
            case "HEAVY_VEHICLE":
                consumption = .08 * rpm;
                break;
            case "MIDDLE_LEVEL_VEHICLE":
                consumption = .10 * rpm;
                break;
            default:
                consumption = .11 * rpm;
                break;
        }
        return consumption;
    }
}

Now let’s see how we use these two classes without a factory.

class ConsumptionWithoutFactory{
    public static void main(String[] args) {
        PetrolCarConsumption petrolCarConsumption = new PetrolCarConsumption();
        String vehicleType = petrolCarConsumption.getVehicleType(1);
        System.out.println("PETROL CONSUMPTION FOR LIGHT VEHICLE= "  + petrolCarConsumption.getFuelConsumption(1000, vehicleType));
        vehicleType = petrolCarConsumption.getVehicleType(2);
        System.out.println("PETROL CONSUMPTION  FOR HEAVY VEHICLE= "  + petrolCarConsumption.getFuelConsumption(2000, vehicleType));
    }
}

This case if implementation was change in interface we have to change all other class as well. Changing work done may depend on the number of classes the interface implementation is instantiated.
Therefore we are using below factory class and let external developers to choose which class type to be used.

class FuelFactory{
    public static FuelCalculation getVehicleCategory(int category){
        if(category == 1)
            return new PetrolCarConsumption();
        else if(category == 2)
            return new DieselCarConsumption();
        return null;
    }
}

Check below example how to use above two classes are going to be used with factory.

class ConsumptionWithFactory{
    public static void main(String[] args) {
        FuelCalculation fuelCalculation = FuelFactory.getVehicleCategory(1);
        if(fuelCalculation != null) {
            String vehicleType = fuelCalculation.getVehicleType(1);
            System.out.println("PETROL CONSUMPTION USING FACTORY FOR LIGHT VEHICLE = "  + fuelCalculation.getFuelConsumption(1000,vehicleType) );
            vehicleType = fuelCalculation.getVehicleType(2);
            System.out.println("PETROL CONSUMPTION USING FACTORY FOR HEAVY VEHICLE = "  + fuelCalculation.getFuelConsumption(2000,vehicleType) );
        } else {
            System.out.println("YOU HAVE NOT PASSED CORRECT VEHICLE CATEGORY. CATEGORY = 1 FOR PETROL, CATEGORY = 2 FOR DIESEL");
        }
    }
}

This scenario always class use factory and no need to change if any changes was happened to interface. We just have to change the factory.
You can download the source code from github here. I have written all classes and interfaces in one file because of easiness of reading to and understanding to you.
https://github.com/dasund/JavaDesignPatterns/blob/master/src/creational/factoryDesignPattern/FuelCalculation.java

5 thoughts on “Factory Design Method

  1. I am sure this piece of writing has touched all the internet people, its really really pleasant paragraph on building up new blog.
    Wow, this piece of writing is good, my younger sister is analyzing such
    things, so I am going to inform her. I could not resist commenting.
    Perfectly written! http://Foxnews.org/

Leave a Reply

Your email address will not be published. Required fields are marked *

WP Facebook Auto Publish Powered By : XYZScripts.com