How to Implement Factory Method In Java

Published on Sep 30,2019 647 Views

How to Implement Factory Method In Java

edureka.co

Classes and Objects are the main part of Java which makes it one of the main programming languages to work with. In this article, we will discuss what is Factory Method In Java in the following order:

 

What is Factory Method in Java?

Factory Pattern or Factory Method in Java says that subclasses are responsible for creating an object of a class. In other words, the Factory Method Pattern is a creational pattern that is used to create objects using factory methods, without having to specify the exact class of the created object.  Factory Methods are also called Virtual Constructors.

In Factory Methods, we create objects without exposing creation logic to the clients. The same common interface is used by the client to create a new type of objects.

 

Advantages of the Factory Method

 

Uses of the Factory Method

 

When to use the Factory Method?

Loose Coupling is introduced Between classes by Factory Method Patterns which is one of the most important principles and should be applied while designing the architecture. Our architecture can be made more flexible and less fragile by introducing loose coupling in program architecture.

 

Here is an Example Code

interface ImageReader {
    DecodedImage getDecodeImage();
}

class DecodedImage {

    private String image;
    public DecodedImage(String image) {
        this.image = image;
    }


    @Override
    public String toString() {
        return image + ": is decoded";
    }
}


class GifReader implements ImageReader {
    private DecodedImage decodedImage;
    public GifReader(String image) {
        this.decodedImage = new DecodedImage(image);
    }


    @Override
    public DecodedImage getDecodeImage() {
        return decodedImage;
    }
}


class JpegReader implements ImageReader {
    private DecodedImage decodedImage;

    public JpegReader(String image) {
        decodedImage = new DecodedImage(image);
    }


    @Override
    public DecodedImage getDecodeImage() {
        return decodedImage;
    }
}


public class FactoryMethodDemo {
    public static void main(String[] args) {
        DecodedImage decodedImage;
        ImageReader reader = null;
        String image = "image.jpeg";
        String format = image.substring(image.indexOf('.') + 1, (image.length()));
        if (format.equals("gif")) {
            reader = new GifReader(image);
        }

        if (format.equals("jpeg")) {
            reader = new JpegReader(image);
        }

        assert reader != null;
        decodedImage = reader.getDecodeImage();
        System.out.println(decodedImage);

    }
}

OUTPUT:

 

Explanation Of Code

This code demonstrates how a factory method is set up. Multiple classes are created with each doing a specific task of decoding an image. We have a driver class called FactoryMethodDemo.

We pass an argument which has to have an extension of .jpeg or .gif etc. Based on the extension the image a class object is created for either jpeg reader or gif reader and execution is done accordingly.

With this, we come to an end of this Factory Method In Java article. I hope you got an understanding of these methods.

Check out the 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. 

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial