FACTORY DESIGN PATTERN IN JAVA

  • By
  • August 2, 2022
  • JAVA
FACTORY DESIGN PATTERN IN JAVA

FACTORY DESIGN PATTERN IN JAVA

 

Q.What is the Factory Method/Factory design pattern in Java and how to use it?

 

-A Java factory pattern helps in creating instances/objects for your classes. The purpose of this factory design pattern is to make the object creation process simpler, modular, and more scalable. The factory method pattern is also called a Virtual constructor of the factory template pattern.

 

As the name signifies Factory, it is a place where different products are created that are similar in features but  divided into categories

 

Mostly we can create objects by using the “New” keyword. but Java factory pattern is a better way to create an instance of the class

 

The creation of an object is not exposed to the client and the client uses the same common interface to create a new type of object. 

 

The core idea behind the static factory method is to create and return instances wherein the details of the class module are hidden from the user at where we have created those instances for the Java classes in Pune.

 

Basically, we cannot create an object/instance of the interface so factory methods are the best way to get the instance of interfaces(means the instance of the implementation classes of interfaces)

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

Example:

 

suppose, you have an interface(e.g Vehical1) and you have multiple implementations (e.g Truck, Bus Bike) for that interface

 

Now you have to instantiate(create the object) of  that interface you can use one of the classes in which that particular interface is implemented 

  

   public static void main(String args[])

     {

  1. g  Vehical1 v1 = new Truck1;

                OR

          Vehical1 v1 = new Bus1;

   OR

        Vehical1 v1 = new Bike1;

     }

 

But, if we don’t want to show the process of object creation to the user, we want to hide that object creation from the user(because a user has the main() method file in which we made all the changes)

 

So instead  of writing the above object creation code in the main method, we can create those objects in another method so that this object creation process is hidden from the client/user, and  that method is called a Factory method

 

That factory method is defined in a separate class. and the whole process is called a Factory design pattern. 

Java is the best programming language, You Need To Learn that skills from The Best Institute. SevenMentor provides the best Java Training in Pune where you can enhance your skills and gain some practical knowledge.

 

//Program that implements the concept of factory design pattern in java

 

1.Vehical1 (the interface has one abstract method run and we can not create an object of this Vehical1 interface)

 

package factory;

 

public interface Vehical1 

{

 

    void run();

}

 

  1. Bike1( The implementation class of Vehical1)

 

package factory;

 

public class Bike1 implements Vehical1

{

public void run()

  {

  System.out.println(“Bike1: run()”);

  }

 

}

 

  1. Bus1( The implementation class of Vehical1)

package factory;

 

public class Bus1 implements Vehical1

{

public void run()

  {

  System.out.println(“Bus1: run()”);

  }

 

}

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

  1. Truck1 ( The implementation class of Vehical1)

 

package factory;

 

public class Truck1  implements Vehical1

{

 

  public void run()

  {

  System.out.println(“truck1: run()”);

  }

 

}

 

}

 

5.VehicalFactoryClass ( The implementation class of Vehical1)

 

package factory;

 

public class VehicalFactoryClass

{

 

 //getVehiFactory(String str) this method is defined here to create the object of the //classes(Truck1, Bus1, Bike) which implements Vehical1 interface

 

  public static Vehical1 getVehiFactory(String str)   {

   

   

      

  if(str.equals(“Truck1”))

    return new Truck1();   //it creates object Truck1

      

 

    else if(str.equals(“Bus1”)) 

    return new Bus1();

        

    else  if(str.equals(“Bike1”)) 

    return new Bike1(); 

        

     

    return null;

       

  }

}

 

  1. FactoryMain

 

package factory;

 

public class FactoryDesignPattern1 {

 

public static void main(String[] args)

{

            

Vehical1 v1= VehicalFactoryClass.getVehiFactory(“Bike1”);       

        v1.run();

}

 

}

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

In the above code,  VehicalFactoryClass.getVehiFactory(“Bike1”);       return the object of Bike1 class and then that object will be stored in v1 reference variable. And then with the help of that object the run() method of “Bike1” class will be called.

 

If we pass “Truck1” as a parameter to the getVehiFactory(“Truck1”); method then in that case this method will return an object of Truck1 class and then that object will be stored into v1. 

 

Author:-

Pooja Nandode-Bhavsar

SevenMentor Pvt Ltd

Call the Trainer and Book your free demo Class for now!!!
call icon

© Copyright 202! | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published.

*
*