Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Factory Method in Java and how to use it?

Published on Sep 06,2019 7.8K Views

65 / 72 Blog from Java Core Concepts

In simple words, a Java factory pattern helps in creating instances for your classes. As the name signifies Factory, it is a place where different products are created that are similar in features yet divided into categories. So, let us dig deeper and understand the concept of Factory method in Java in detail.

Here are the topics that will be covered in this tutorial:

Let’s begin!

Starting off with the definition of a Java factory pattern.

What is Java factory pattern?

A quick question. How do you create an instance of a class in Java? Mostly by using the “New” keyword. Well, you have a better method here and this is what Java factory pattern is all about.

Factory pattern is used to create instances for classes. 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.

In a nutshell, a superclass will specify all the standard and generic behaviors and then delegates the creation details to the subclasses which are supplied by the client.

After understanding the actual meaning of the factory method pattern in Java, let us understand the advantages that it offers.

Advantages of Java factory method

I am enumerating a few of the advantages which Java factory method offers:

  1. The object that you create can be used without duplication of code.
  2. If you use a factory method instead of a constructor, the factory method can have different and more descriptive names also .
  3. Also, it removes the instantiation of the implementation classes from the client code.
  4. This method makes the code more robust, less coupled and easy to expand.
  5. Factory pattern through inheritance provides abstraction between implementation and the client classes.

After learning about the advantages, let us move towards our next segment of this article.

Factory pattern implementation

Now, the implementation process might look a bit complex to you. Follow my instructions to understand the procedure step by step.

Firstly, I’m going to create a shape interface and also the respective concrete classes that would implement the shape interface. After that, a factory class is defined.

First of all, an interface is created:

 public interface Shape 
{ 
void draw(); 
} 

Then, the concrete classes are created to implement the interface in this manner:

 public class Rectangle implements Shape 
{ 
@Override public void draw() 
{ 
System.out.println("Rectangle::draw() method."); 
} 
} 
public class Square implements Shape 
{ 
@Override public void draw() 
{ 
System.out.println("Square::draw() method."); 
} 
}
public class Circle implements Shape 
{ 
@Override public void draw() 
{
System.out.println("Circle::draw() method."); 
} 
} 

After creating concrete classes, a factory is created to generate the objects.

 public class ShapeFactory 
{ 
//use getShape method to get object of type shape 
public Shape getShape(String shapeType)
{ 
if(shapeType == null)
{ 
return null; 
} 
if(shapeType.equalsIgnoreCase("CIRCLE"))
{ 
return new Circle(); 
} 
else if(shapeType.equalsIgnoreCase("RECTANGLE"))
{ 
return new Rectangle(); 
} 
else if(shapeType.equalsIgnoreCase("SQUARE"))
{ 
return new Square(); 
} 
return null; 
} 
} 

Now, the factory is created to use the objects of the concrete classes to pass the required information:

 public class FactoryPatternDemo
{ 
public static void main(String[] args) 
{ 
ShapeFactory shapeFactory = new ShapeFactory();
//get an object of Circle and call its draw method. 
Shape shape1 = shapeFactory.getShape("CIRCLE"); 
//call draw method of Circle shape1.draw();
//get an object of Rectangle and call its draw method. 
Shape shape2 = shapeFactory.getShape("RECTANGLE"); 
//call draw method of Rectangle 
shape2.draw(); 
//get an object of Square and call its draw method.
Shape shape3 = shapeFactory.getShape("SQUARE"); 
//call draw method of square 
shape3.draw(); 
} 
}

Output:

Circle::draw() method.
Rectangle::draw() method.
Square::draw() method.

This is how you implement the factory pattern method through Java code.

I hope the above-mentioned content proved to be helpful in enhancing your Java knowledge. Keep reading, keep exploring!

Also check out Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please do mention them in the comments section of this “Factory Method in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 19th October,2024

19th October

SAT&SUN (Weekend Batch)
View Details
Java Course Online

Class Starts on 14th December,2024

14th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Factory Method in Java and how to use it?

edureka.co