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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Implement Factory Method In Java

Published on Sep 30,2019 10 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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. 

Comments
0 Comments

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.