# Abstract Factory

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Abstract Factory (which is a Creational Pattern).

# Abstract Factory - theory

The Abstract Factory Java pattern is used when we need to create a factory of factories. Supposing at one point in the application, function of some events we need to create (use) a factory or another. This pattern let us choose in the code which factory to use in order to create particular objects. In order to understand the concept of Abstract Factory in Java, you have to take a look at the concept of Factory Method pattern as well.

In order to understand the concept of Abstract Factory in Java, please take a look at the UML diagram for the Abstract Factory pattern:

# Abstract Factory - example

For testing this pattern here it is an example:

public interface Animal {

    public void setAge(int age);
    public String getAge();

}
public class Rabbit implements Animal {

    int age;
     
    public Rabbit() {
        super();
        this.age = 0;
    }
     
    @Override
    public void setAge(int age) {
        this.age = age;
    }
 
    @Override
    public String getAge() {
        return "The RABBIT has "+age+" years old.";
    }
}
public class Cow implements Animal {

    public Cow() {
        super();
        this.age = 0;
    }
 
    int age;
     
    @Override
    public void setAge(int age) {
        this.age = age;
    }
 
    @Override
    public String getAge() {
        return "The COW has "+age+" years old.";
    }
}
public class Horse implements Animal {

    int age;
     
    public Horse() {
        super();
        this.age = 0;
    }
     
    @Override
    public void setAge(int age) {
        this.age = age;
    }
 
    @Override
    public String getAge() {
        return "The HORSE has "+age+" years old.";
    }
}
public interface Car {
  public void draw();
}
public class Dacia implements Car{

    public void draw() {
        System.out.println("A DACIA has been drawn !");
    }
}
public class Chevrolet implements Car{

    public void draw() {
        System.out.println("A CHEVROLET has been drawn !");
    }
}
public class Mercedes implements Car{

    public void draw() {
        System.out.println("A MERCEDES has been drawn !");
    }
}
public interface AbstractFactory {

    public Animal getAnimal(String animalType);
    public Car getCar(String carName);

}
public class AnimalsFactory implements AbstractFactory {

    @Override
    public Animal getAnimal(String animalType) {
         
        if(animalType == null){
             return null;
          }     
          if(animalType.equalsIgnoreCase("RABBIT")){
             return new Rabbit();
              
          } else if(animalType.equalsIgnoreCase("COW")){
             return new Cow();
              
          } else if(animalType.equalsIgnoreCase("HORSE")){
             return new Horse();
          }
           
          return null;
    }
     
    @Override
    public Car getCar(String carName) {
        return null;
    }
}
public class CarFactory implements AbstractFactory {

    @Override
    public Car getCar(String carName) {
         
        if(carName == null){
             return null;
          }     
          if(carName.equalsIgnoreCase("DACIA")){
             return new Dacia();
              
          } else if(carName.equalsIgnoreCase("MERCEDES")){
             return new Mercedes();
              
          } else if(carName.equalsIgnoreCase("CHEVROLET")){
             return new Chevrolet();
          }
           
          return null;
    }
     
    @Override
    public Animal getAnimal (String animalType) {
        return null;
    }
}
public class FactoryProducer {

  public static AbstractFactory getFactory(String choice){
  
        if(choice.equalsIgnoreCase("ANIMAL")){
           return new AnimalsFactory();
            
        }else if(choice.equalsIgnoreCase("CAR")){
           return new CarFactory();
        }
         
        return null;
  }
}
public class AbstractFactoryExample {

    public static void main(String[] args) {
         
        //
        AbstractFactory carFactory = FactoryProducer.getFactory("CAR");
 
        Car car1 = carFactory.getCar("CHEVROLET");
        car1.draw();
         
        AbstractFactory animalFactory = FactoryProducer.getFactory("ANIMAL");
 
        Animal animal1 = animalFactory.getAnimal("RABBIT");
        animal1.setAge(2);
        System.out.println(animal1.getAge());
         
    }
}

When I run my example, I get the following result: