First of all you have to read Factory Design Pattern before you learn about Abstract Factory Design Pattern. You can go through factory design Pattern article with this.
abstract-factory-design-pattern

Now let’s move to Abstract factory design method explanation.
Abstract factory design pattern is categorized into creational design patterns category. This provides an interface for creating dependent objects without specifying there concrete classes.
This pattern is being used while you need to create multiple families of objects and when you need to hide the implementations.
Check below example which shows to get costs from bus ride vs Uber ride.
First we have to write Abstract Interface for cost assigning and concrete classes for bus and Uber rides.

/**
 * CREATED THE ABSTRACT COST INTERFACE HERE.
 */
public interface TravellingCost {
    double setCostPerKM();
}
/**
 * CREATING FIRST CONCRETE COST CLASSES FOR BUS TRAVELLERS.
 */
class BusTravellingCost implements TravellingCost{
    @Override
    public double setCostPerKM() {
        return 100;
    }
}
/**
 * CREATING SECOND CONCRETE COST CLASSES FOR UBER TAXI TRAVELLERS.
 */
class UberTravellingCost implements TravellingCost{
    @Override
    public double setCostPerKM() {
        return 150;
    }
}

Now I’m going to create Abstract factory interface and related Concrete factory classes for Bus ride and Uber ride.

/**
 * CREATING ABSTRACT FACTORY
 */
interface AbstractCostFactory{
    TravellingCost generateCost();
}
/**
 * CREATING CONCRETE FACTORY FOR BUS TRAVELLERS
 */
class BusCostFactory implements AbstractCostFactory{
    public TravellingCost generateCost(){
        BusTravellingCost busTravellingCost  = new BusTravellingCost();
        return busTravellingCost;
    }
}
/**
 * CREATING CONCRETE FACTORY FOR UBER TRAVELLERS
 */
class UberCostFactory  implements AbstractCostFactory{
    public TravellingCost generateCost(){
        UberTravellingCost uberTravellingCost = new UberTravellingCost();
        return uberTravellingCost;
    }
}

If you look at above two factory classes they are returning TravellingCost interfaces but such as BusTravellingCost and UberCostFactory. Then we have to write a class which returns a TravellingCost object but inside the object can be BusTravellingCost or UberTravellingCost according to passed parameter AbstractCostFactory type.

/**
 * NOW CREATING TRAVELLER CLASS WHICH METHOD SEND TRAVELLINGCOST OBJECT ACRODING TO PASSING COST FACTORY TYPE.
 */
class Traveller{
    public double getCheapestRide(AbstractCostFactory abstractCostFactory){
        TravellingCost travellingCost = abstractCostFactory.generateCost();
        return travellingCost.setCostPerKM();
    }
}
/**
 * THIS CLASS CALLED TRAVELLED CLASS TO GET THE COST BUT WE HAVE TO PASS RELEVANT FACTORY AS A PARAMETER.
 */
class Main{
    public static void main(String[] args) {
        Traveller traveller = new Traveller();
        //GET BUS COST
        AbstractCostFactory abstractBusCostFactory = new BusCostFactory();
        System.out.println( "BUS COST = " + traveller.getCheapestRide(abstractBusCostFactory)  );
        //GET UBER COST
        AbstractCostFactory abstractUberCostFactory = new UberCostFactory();
        System.out.println( "UBER COST = " + traveller.getCheapestRide(abstractUberCostFactory)  );
    }
}

6 thoughts on “Abstract Factory Design Pattern Java

  1. My brother recommended I may like this website. He used to be totally right. This post truly made my day. You can not consider simply how so much time I had spent for this info! Thanks! aebcgbbbkkee

  2. I see you don’t monetize your website, don’t
    waste your traffic, you can earn extra cash every month because you’ve got high quality content.
    If you want to know how to make extra bucks, search for: best adsense alternative Wrastain’s tools

    1. Dear @86Neva – Thank you very much for your comment and helpful information. Yes I have not monetize my web site and I do not like adsense personally because I have tried to activate adsence for my first blog http://codehelpdesk.blogspot.com/ . But they did not accept it and currently my adsence account is blocked since I have request activation multiple times. Therefore I will definitely consider about your adsence alternative suggestion. Regards.

Leave a Reply

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

WP Facebook Auto Publish Powered By : XYZScripts.com