Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll learn how to implement the factory pattern with generics in Java.

2. What Is Factory Pattern?

In object-oriented programming, the factory pattern is a creational design pattern that is responsible for creating objects when it's called.

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

Factory

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

3. How Is It Implemented?

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

ClassDiagram

Now let's implement each class in the diagram.

3.1. Implementing the Notifier Interface

The Notifier interface is a prototype, and other notifier classes implement it:

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

As we can see, the Notifier class is a generic class that has one method named notify.

3.2. Implementing the Notifier Classes

Now let's implement the 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 the Notifier interface – one that will output a simple text and one that will post a date.

3.3. Implementing the Factory

The factory class generates a notifier instance each time its only method, getNotifier(), 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. Implementing the Record

The Record enum keeps the record of valid notifier classes and creates an instance each time the factory class calls it:

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've successfully implemented the factory pattern.

4. Using the Factory

Let's use the factory in our Main class:

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 over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
4 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!