# Factory Method

In 
Published 2022-12-03

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

# Factory Method - theory

The Factory (Factory Method or Virtual Constructor) Design Pattern is probably the most used design pattern in Java. In Factory Method pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. In Factory Method Pattern, we define an interface (or an abstract class) for creating an object, but let subclasses instantiate the object.

# Factory Method - example

Here is the UML Diagram for the Factory Method Design Pattern (for my example):

Animal.java
public interface Animal {

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

}
Rabbit.java
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.";
    }
}
Cow.java
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.";
    }
}
Horse.java
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.";
    }
}
AnimalsFactory.java
public class AnimalsFactory {
 
    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;
         
    }
}
FactoryExampleMain.java
public class FactoryExampleMain {
 
    public static void main(String[] args) {
         
        AnimalsFactory animalsFactory = new AnimalsFactory();
         
        Animal animal1 = animalsFactory.getAnimal("COW");
        animal1.setAge(3);
         
        Animal animal2 = animalsFactory.getAnimal("RABBIT");
        animal2.setAge(2);
         
        Animal animal3 = animalsFactory.getAnimal("HORSE");
        animal3.setAge(4);
         
        System.out.println(animal1.getAge());
        System.out.println(animal2.getAge());
        System.out.println(animal3.getAge());
    }
}

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