Browse Java Design Patterns & Enterprise Application Architecture

Registry Pattern in Java Use Cases and Examples in Java

Identify when a Java registry is a focused lookup solution and when it is drifting into service-location or hidden global wiring.

Registry use case: A situation where key-based lookup is a real requirement and centralizing that mapping improves clarity.

Registry works well when the problem is genuinely “given a key, find the right handler or object from one bounded family.”

Strong Use Cases

Good Java registry use cases include:

  • message-type to handler dispatch
  • content-type to serializer resolution
  • export-format to formatter lookup
  • plugin identifier to plugin provider mapping

In all of these, lookup by key is central to the problem.

Weak Use Cases

Registry is a weak trade when:

  • the caller already knows the dependency directly
  • the registry becomes a way to avoid explicit injection
  • unrelated services share the same registry
  • the key-based access is mostly accidental rather than meaningful

If lookup is not the real problem, a registry is usually unnecessary indirection.

Example: Serializer Registry

 1public interface Serializer {
 2    byte[] serialize(Object value);
 3}
 4
 5public final class SerializerRegistry {
 6    private final Map<String, Serializer> serializers;
 7
 8    public SerializerRegistry(Map<String, Serializer> serializers) {
 9        this.serializers = Map.copyOf(serializers);
10    }
11
12    public Serializer serializerFor(String contentType) {
13        Serializer serializer = serializers.get(contentType);
14        if (serializer == null) {
15            throw new IllegalArgumentException("Unsupported content type: " + contentType);
16        }
17        return serializer;
18    }
19}

This works because the registry solves one clear lookup problem and stays bounded.

Design Review Questions

When reviewing registry use cases, ask:

  • Is key-based lookup really central to the problem?
  • Are the entries one conceptual family?
  • Would direct injection or a factory be simpler?
  • Does the registry reduce branching, or just relocate it?

A good registry makes a real lookup problem explicit. A bad registry is just global indirection with a nicer name.

Loading quiz…
Revised on Thursday, April 23, 2026