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.
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.
Use a Prototype Registry when:
Typical Java examples include document templates, workflow presets, seeded test data objects, product configuration defaults, or notification layouts.
A good registry owns:
It should not quietly become:
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.
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:
If creation logic is dynamic, parameter-heavy, or not template-oriented, a factory is usually the cleaner tool.
When reviewing a Prototype Registry in Java, ask:
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.