Browse Java Design Patterns & Enterprise Application Architecture

Implementing the Registry Pattern in Java

Implement Registry in Java as a focused lookup boundary rather than a hidden global dependency mechanism.

Registry: A component that maps keys to objects, factories, or handlers and provides controlled lookup for those entries.

Registry is useful when callers need to choose behavior by key, type, or identifier without hard-coding every mapping. That often appears in plugin systems, serializer dispatch, message handlers, or export format selection.

A Focused Java Example

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

This is a healthy registry because:

  • the scope is explicit
  • the entries are focused on one problem
  • lookup rules are obvious
  • the registry itself can still be injected normally

What Makes Registry Dangerous

Registry starts drifting into trouble when:

  • unrelated services accumulate in one global map
  • callers use it as a substitute for dependency injection
  • lifecycle and ownership become hidden behind lookup
  • any class can fetch almost anything at runtime

At that point, it begins to look more like service locator than focused registry.

Design Review Questions

When reviewing a registry, ask:

  • What single lookup problem does this registry solve?
  • Are entries constrained to one conceptual family?
  • Is the registry itself owned explicitly, or globally reachable?
  • Would direct injection or factory selection be simpler?

A good registry is a disciplined index. A bad one is an unbounded dependency warehouse.

Loading quiz…
Revised on Thursday, April 23, 2026