Factory Design Pattern

Factory Design pattern is one of the most popular creational design pattern. Factory pattern is used to create different types of objects without specifying the exact class of object that will be created. Sometimes creating an object requires complex processes not appropriate to include within client object.

Factory class abstract the complex logic of object creation from client and and provides a common interface for client to refers to the any newly created object. In Factory method, subclasses are responsible to create the object of the class.

Advantages of Factory Pattern

  • The complex logic of object creation is hidden from client.
  • We can modify factory class to create new objects without affecting client code.
  • It provides a single authoritative source of creating objects. A single factory class can be used across various software components to maintain consistency.
  • It allows the subclasses to choose the type of objects to create.

Implementation of Factory Design Pattern

We will create an interface called ‘Bird’ containing ‘fly()’ method. Then we will define concrete classes “Owl”, “Eagle” and “Sparrow” implementing Bird interface.

Factory_Pattern

Factory Design Pattern

Bird.java
public interface Bird {
    void fly();
}
Owl.java
public class Owl implements Bird {
 
   @Override
   public void fly() {
      System.out.println("Owl is Flying");
   }
}
Eagle.java
public class Eagle implements Bird {
 
   @Override
   public void fly() {
      System.out.println("Eagle is Flying");
   }
}
Sparrow.java
public class Sparrow implements Bird {
 
   @Override
   public void fly() {
      System.out.println("Sparrow is Flying");
   }
}

Now, We will create a factory class BirdFactory.java to create objects of specific bird types based on given input parameter.

Factory Design Pattern 1

BirdFactory.java
public class BirdFactory {
  
    //getBird is a factory method to get various bird objects 
    public Bird getBird(String birdSpecies){
     if(birdSpecies == null){
         return null;
     }  
     if(birdSpecies.equalsIgnoreCase("EAGLE")){
         return new Eagle();
     } else if(birdSpecies.equalsIgnoreCase("OWL")){
         return new Owl();          
     } else if(birdSpecies.equalsIgnoreCase("SPARROW")){
         return new Sparrow();
     } else {
         return null;
     }
    }
}

We are creating a BirdSanctuary.java to demo the working of factory class. It will create an instance of factory class and use it to create various bird objects.

Factory Design Pattern 2

BirdSanctuary.java
public class BirdSanctuary {
 
    public static void main(String[] args) {
     // Create a factoru class instance 
     BirdFactory factory = new BirdFactory();
 
        //get Owl object typecasted to Bird Interface.
        Bird owl = factory.getBird("OWL");
         
        //get Sparrow object typecasted to Bird Interface.
        Bird sparrow = factory.getBird("SPARROW");
         
        //get EAGLE object typecasted to Bird Interface.
        Bird eagle = factory.getBird("EAGLE");
 
        // Call fly function of all birds
        owl.fly();
        sparrow.fly();
        eagle.fly();
   }
}

Output

Owl is Flying
Sparrow is Flying
Eagle is Flying
Important Points About Factory Pattern

  • Clients create objects using factory instead of directly creating it using new operator. It calls factory object and specify what type of object is needed.
  • Factory pattern creates various objects without exposing the complex logic of object creation.
  • The factory method returns newly created object as requested by client after type casting it to a common interface.
  • Client interact with object through interface, without being aware of the type of concrete class.