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

How to Implement Factory Method In Java

Published on Sep 30,2019 770 Views


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.

Factory Method in Java Logo

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

  • The type of objects to be created are chosen by the subclasses. The Factory Method allows this.

  • By eliminating the need to bind application-specific classes in the code, it promotes loose coupling.

 

Uses of the Factory Method

  • They are used when the class has no idea of what subclasses are required.

  • They are used when a class wants subclasses to specify objects that need to be created.

  • Parents classes choose the creation of objects of subclasses, we use factory methods.

 

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:

Factory Method In Java

 

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 Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th 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!

How to Implement Factory Method In Java

edureka.co