Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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.
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.
I am enumerating a few of the advantages which Java factory method offers:
After learning about the advantages, let us move towards our next segment of this article.
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 28th September,2024 28th September SAT&SUN (Weekend Batch) | View Details |
edureka.co