Files
Mughees Qasim 3ae6b07590 refactoring: Issue #2377: The repository code has been refactored to use the recor… (#2505)
* Issue #2377: The repository code has been refactored to use the record class

* Issue #2377: Refactored according to the rules defined for the repo code

* Issue #2377: Refactored according to the rules defined for the repo code

* Issue #2377: Refactored according to the rules defined for the repo code
2023-05-06 12:08:12 +03:00
..
2020-12-08 10:34:42 +05:30
2022-09-14 23:22:24 +05:30

title, category, language, tag
title category language tag
Registry Creational en
Instantiation

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

public class Customer {

  private final String id;
  private final String name;

  public Customer(String id, String name) {
    this.id = id;
    this.name = name;
  }

  public String getId() {
    return id;
  }

  public String getName() {
    return name;
  }

}

This registry of the Customer objects is CustomerRegistry

public final class CustomerRegistry {

  private static final CustomerRegistry instance = new CustomerRegistry();

  public static CustomerRegistry getInstance() {
    return instance;
  }

  private final Map<String, Customer> customerMap;

  private CustomerRegistry() {
    customerMap = new ConcurrentHashMap<>();
  }

  public Customer addCustomer(Customer customer) {
    return customerMap.put(customer.getId(), customer);
  }

  public Customer getCustomer(String id) {
    return customerMap.get(id);
  }

}

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