Files
java-design-patterns/registry
Rahul Raj fb7ec9b375 dependencies: #2449 bump maven-checkstyle-plugin from 3.1.0 to 3.2.0 + resolve chec… (#2464)
* #2449 bump maven-checkstyle-plugin from 3.1.0 to 3.2.0 + resolve checkstyle issues

* remove FileSelectorJFrame.java to resolve checkstyle issue

* remove FileSelectorJFrame.java to resolve checkstyle issue

* remove FileSelectorJFrame.java to resolve checkstyle issue

* add refactored file with correct filename to resolve checkstyle issue

* add the test data

* change filenames from JFrame to Jframe for checkstyle

* fix code smell from  sonar report

* add new testcases to improve the test coverage

* remove code smell
2023-02-04 19:20:54 +02: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