Implementing Factory Pattern with Generics in Java

1. Overview

In this tutorial, we will learn how to implement Factory Pattern with Generics in Java.

2. What is Factory Pattern?

In object-oriented programming, the factory pattern is a constructive design pattern that is responsible for creating an object when it is called.

A factory is a class that creates objects of a prototype class, aka interface, from a method call:

Factory pattern is good when we want to create objects of a common interface while hiding the construction logic from the user.

3. How is it implemented?

Now let’s see how to implement it. First, let’s take a look at the class diagram:

class diagram

Now let’s implement each class in the diagram.

3.1. To apply please inform interface

please inform An interface is a prototype, and other Notifier classes implement it:

public interface Notifier<T> {
    void notify(T obj);
}

as we can see, please inform class is a generic class with a method named please inform,

3.2. To apply please inform classrooms

Now let’s implement two other notifier classes:

public class StringNotifier implements Notifier<String> {
    @Override
    public void notify(String str) {
        System.out.println("Notifying: " + str);
    }
}
public class DateNotifier implements Notifier<Date> {
    @Override
    public void notify(Date date) {
        System.out.println("Notifying: " + date);
    }
}

Now we have two classes that use please inform Interface – one that will produce a simple text and one that will post a date.

3.3. implement factory

The factory class generates a notifier instance each time for its only method, getnotifier()It is called:

public class NotifierFactory {
    public <T> Notifier<T> getNotifier(Class<T> c) {
        if (c == String.class) {
            return Record.STRING.make();
        }
        if (c == Date.class) {
            return Record.DATE.make();
        }
        return null;
    }
}

In the above code, record is an enum with two constants named string And Date,

3.4. To apply record

record The enum holds a record of the valid notifier class and creates an instance each time the factory class is called,

public enum Record {
    STRING {
        @Override
        public Notifier<String> make() {
            return new StringNotifier();
        }
    },
    DATE {
        @Override
        public Notifier<Date> make() {
            return new DateNotifier();
        }
    };
    public abstract <T> Notifier<T> make();
}

We have successfully implemented the factory pattern.

4. Using Factory

let’s use our factory Main square:

public static void main(String[] args) {
    NotifierFactory factory = new NotifierFactory();
    Notifier<String> stringNotifier = factory.getNotifier(String.class);
    Notifier<Date> dateNotifier = factory.getNotifier(Date.class);
    stringNotifier.notify("Hello world!");
    dateNotifier.notify(new Date());
}

Now we should compile and run our code:

$ javac Main.java
$ java Main
Notifying: Hello world!
Notifying: Wed Oct 19 17:36:38 TRT 2022

As we can see, the factory has successfully created two notifier instances of the appropriate type.

5. Summary

In this article, we learned how to implement and use the Factory pattern in Java.

As always, the source code is available on GitHub.

       

Leave a Comment