Browser docs

Registry

Intent

Stores the objects of a single class and provide a global point of access to them. Similar to Multiton pattern, only difference is that in a registry there is no restriction on the number of objects.

Explanation

In Plain Words

Registry is a well-known object that other objects can use to find common objects and services.

Programmatic Example Below is a Customer Class

 1public class Customer {
 2
 3  private final String id;
 4  private final String name;
 5
 6  public Customer(String id, String name) {
 7    this.id = id;
 8    this.name = name;
 9  }
10
11  public String getId() {
12    return id;
13  }
14
15  public String getName() {
16    return name;
17  }
18
19}

This registry of the Customer objects is CustomerRegistry

 1public final class CustomerRegistry {
 2
 3  private static final CustomerRegistry instance = new CustomerRegistry();
 4
 5  public static CustomerRegistry getInstance() {
 6    return instance;
 7  }
 8
 9  private final Map<String, Customer> customerMap;
10
11  private CustomerRegistry() {
12    customerMap = new ConcurrentHashMap<>();
13  }
14
15  public Customer addCustomer(Customer customer) {
16    return customerMap.put(customer.getId(), customer);
17  }
18
19  public Customer getCustomer(String id) {
20    return customerMap.get(id);
21  }
22
23}

Class diagram

Registry

Applicability

Use Registry pattern when

  • client wants reference of some object, so client can lookup for that object in the object’s registry.

Consequences

Large number of bulky objects added to registry would result in a lot of memory consumption as objects in the registry are not garbage collected.

Credits