DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Enabling Behavior-Driven Service Discovery: A Lightweight Approach to Augment Java Factory Design Pattern
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Trending

  • Kill the Worker, Keep the Research: Build a Recoverable LangGraph Agent on Temporal
  • How to Format Articles for DZone
  • Building a Practical Cloud-Native Golden Path: A Guide to Kubernetes-Based Service Delivery, Self-Service, and Developer-Friendly Defaults
  • AI-Era Cybersecurity, Part 1: Mapping Glasswing, Daybreak, Lightwell and the Tools Racing to Find and Fix Bugs

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. A Closer Look at the Java Generic Factory Pattern

A Closer Look at the Java Generic Factory Pattern

Check out these example implementations of the Java generic factory pattern!

By 
Konrad Eichstädt
·
Oct. 31, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
36.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introducing the Java Generic Factory

Read on to learn more about the Java generic factory pattern!

During my last project, I was required to build complex objects based on values derived from an XML File. We decided to use the Builder Pattern to help build complex objects.

For complex objects, which are aggregates of Domain-Driven Design, the building process, in the end, is a long method that builds all parts of the complex object. I was looking for a pattern that would allow me to create different components using only one method.

You may also like: Understanding the Use Cases of Java Generics

The first step was to create a functional interface as an implementation contract for all part factories.

public interface ElementFactory<T> {

  T build(Document document, XPATHHidaValues xpathHidaValues) throws Exception;

}


The return type of the build method is a generic type with the ability to collect different parts of the objects. The parameters document and XPATHHidaValues can be ignored because these objects are only relevant when getting the attribute values from an XML document using XPATH. For your project, you can use whatever you need to create different objects.

The next step was to create a factory registry to get the relevant factory.

public class ElementFactoryRegistry {

  private static Map<Class<?>, ElementFactory<?>> elementBuilder = new HashMap<>();

  static {

    elementBuilder.put(Schreibsprache.class, new SchreibSpracheFactory());
    elementBuilder.put(Material.class, new MaterialListFactory());
    elementBuilder.put(Abmessungen.class, new AbmessungFactory());
    elementBuilder.put(Format.class, new FormatFactory());
    elementBuilder.put(Umfang.class, new UmfangFactory());
    elementBuilder.put(Schrift.class, new SchriftenListFactory());

  }

  public static <T> T buildElement(Type targetType, Document document,
      XPATHHidaValues xpathHidaValues)
      throws Exception {

    if (!elementBuilder.containsKey(targetType)) {

      throw new IllegalArgumentException("Missing Element Factory for Type " + targetType);
    }

    return (T) elementBuilder.get(targetType).build(document, xpathHidaValues);
  }
}


As you can see, the registry object is mainly static. For the registration of all factories, I used a HashMap. The key is the needed target class, which should be created as part of the complex aggregate. The value is the proper element factory class. Both elements of the map are typed using the generic wildcard character.

Additionally, you can see that the build element method checks to see if the factory is registered using a generic cast to build the needed class component. The main advantage of this is that the client doesn’t need any cast and can use the method at different points.

BeschreibungsKomponenteAeusseres aeusseres = new BeschreibungsKomponenteAeusseresBuilder()
    .withId(UUID.randomUUID().toString())
    .withMaterialen(
        buildElement(Material.class, document, xpathHidaValues))
    .withAbmessungen(buildElement(Abmessungen.class, document, xpathHidaValues))
    .withFormat(buildElement(Format.class, document, xpathHidaValues))
    .withUmfang(buildElement(Umfang.class, document, xpathHidaValues))
    .withSchriften(buildElement(Schrift.class, document, xpathHidaValues))
    .build();


Here, you can see the magic happen — you now have the Java generics type system. The client is using the builder pattern and can use the magic factory method buildElement to build the proper object. Based on the target type class, this method selects the proper factory and creates the relevant object type.

 At first, it will create a Material, and the next time, it will create a Format object. From my point of view, the code is very clean and readable so that can every other developer on the team can easily add new factories. 

Further Reading

Java: The Factory Method Pattern

Understanding the Use Cases of Java Generics

Factory (object-oriented programming) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Enabling Behavior-Driven Service Discovery: A Lightweight Approach to Augment Java Factory Design Pattern
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook