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.”
Good Java registry use cases include:
In all of these, lookup by key is central to the problem.
Registry is a weak trade when:
If lookup is not the real problem, a registry is usually unnecessary indirection.
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.
When reviewing registry use cases, ask:
A good registry makes a real lookup problem explicit. A bad registry is just global indirection with a nicer name.