Browse Java Design Patterns & Enterprise Application Architecture

Prototype Registry

Use a Prototype Registry in Java when named templates should produce safe copies without pushing configuration logic into every caller.

Prototype registry: A registry that stores named prototype objects or factories and returns new copies when callers request them.

A Prototype Registry is useful when callers should choose from a controlled set of templates instead of reconstructing each variation manually. In Java, it works best when the prototypes are immutable or copied through explicit, tested logic.

The Core Idea

Instead of exposing every construction detail, the registry exposes stable template names:

 1public final class NotificationTemplateRegistry {
 2    private final Map<String, NotificationTemplate> templates;
 3
 4    public NotificationTemplateRegistry(Map<String, NotificationTemplate> templates) {
 5        this.templates = Map.copyOf(templates);
 6    }
 7
 8    public NotificationTemplate copyOf(String key) {
 9        NotificationTemplate template = templates.get(key);
10        if (template == null) {
11            throw new IllegalArgumentException("Unknown template: " + key);
12        }
13        return new NotificationTemplate(template);
14    }
15}

The registry does not hand out the original template. It hands out a fresh copy based on a named source.

When A Registry Helps

Use a Prototype Registry when:

  • the application has a stable set of common templates
  • callers should choose a variation by name, type, or key
  • repeated setup logic would otherwise spread across many call sites
  • the copy behavior is already well-defined

Typical Java examples include document templates, workflow presets, seeded test data objects, product configuration defaults, or notification layouts.

What The Registry Should Own

A good registry owns:

  • lookup by stable identifier
  • validation that the template exists
  • safe copy production
  • optional bootstrap-time registration

It should not quietly become:

  • a service locator for unrelated runtime collaborators
  • a mutable global bag of random objects
  • a place where clients receive shared mutable instances by accident

Thread Safety And Mutability

The easiest registry to reason about is bootstrapped once and then read-only. If templates are immutable, the registry can share them internally and create cheap derived copies or variations.

If templates are mutable, the copy rule becomes much more important. Returning the original object instead of a copy is one of the most common Prototype Registry bugs.

Registry Versus Factory

A factory decides how to create an object. A prototype registry decides which preconfigured starting point to copy.

That means a registry is a better fit when:

  • the available variants are finite and named
  • each variant is already configured
  • callers do not need to know the construction recipe

If creation logic is dynamic, parameter-heavy, or not template-oriented, a factory is usually the cleaner tool.

Design Review Questions

When reviewing a Prototype Registry in Java, ask:

  • Does the registry return copies or shared instances?
  • Are template keys stable and intentional?
  • Is registration controlled at bootstrap, or can random code mutate the registry?
  • Would a factory or configuration object be clearer than a registry?

Prototype Registry is strong when the domain genuinely has reusable templates. It becomes messy when “registry” is just a softer name for uncontrolled global state.

Loading quiz…
Revised on Thursday, April 23, 2026