Skip to content

Commit e731bdb

Browse files
committed
docs: update registry
1 parent cfbb5af commit e731bdb

File tree

2 files changed

+105
-22
lines changed

2 files changed

+105
-22
lines changed

registry/README.md

Lines changed: 103 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,47 @@ title: Registry
33
category: Creational
44
language: en
55
tag:
6-
- Instantiation
6+
- API design
7+
- Data access
8+
- Decoupling
9+
- Dependency management
10+
- Enterprise patterns
11+
- Instantiation
712
---
813

914
## Intent
10-
Stores the objects of a single class and provide a global point of access to them.
11-
Similar to Multiton pattern, only difference is that in a registry there is no restriction on the number of objects.
15+
16+
To centralize the creation and management of a global set of objects, providing a single point of access and ensuring controlled instantiation.
1217

1318
## Explanation
1419

20+
Real-world example
21+
22+
> In a large corporation, managing IT assets such as laptops, desktops, servers, and software licenses can be challenging. To streamline this process, the company uses a centralized IT Asset Management System, which functions as a Registry.
23+
>
24+
> * The system provides a single point of access for registering, tracking, and retrieving information about all IT assets.
25+
> * When a new device or software is procured, it is registered in the system with details such as serial number, purchase date, warranty period, and assigned user.
26+
> * IT staff can query the system to get details about any asset, check its current status, and update information as needed.
27+
> * This centralized management promotes efficient utilization and maintenance of assets, ensures compliance with software licenses, and helps in planning for upgrades or replacements.
28+
>
29+
> In this analogy, the IT Asset Management System acts as a Registry, managing the lifecycle and providing global access to information about IT assets within the organization.
30+
1531
In Plain Words
1632

1733
> Registry is a well-known object that other objects can use to find common objects and services.
1834
35+
wiki.c2.com says
36+
37+
> A registry is a global association from keys to objects, allowing the objects to be reached from anywhere. It involves two methods: one that takes a key and an object and add objects to the registry and one that takes a key and returns the object for the key. It's similar to the MultitonPattern, but doesn't restrict instances to only those in the registry.
38+
1939
**Programmatic Example**
20-
Below is a `Customer` Class
40+
41+
The Registry design pattern is a well-known pattern used in software design where objects are stored and provide a global point of access to them. This pattern is particularly useful when you need to manage a global collection of objects, decouple the creation of objects from their usage, ensure a controlled lifecycle for objects, and avoid redundant creation of objects.
42+
43+
First, we have the `Customer` class. This class represents the objects that will be stored in the registry. Each `Customer` has an `id` and a `name`.
2144

2245
```java
2346
public class Customer {
24-
2547
private final String id;
2648
private final String name;
2749

@@ -30,35 +52,43 @@ public class Customer {
3052
this.name = name;
3153
}
3254

33-
public String getId() {
55+
public String id() {
3456
return id;
3557
}
3658

37-
public String getName() {
59+
public String name() {
3860
return name;
3961
}
4062

63+
@Override
64+
public String toString() {
65+
return "Customer{" +
66+
"id='" + id + '\'' +
67+
", name='" + name + '\'' +
68+
'}';
69+
}
4170
}
4271
```
4372

44-
This registry of the `Customer` objects is `CustomerRegistry`
73+
Next, we have the `CustomerRegistry` class. This class is the actual registry where `Customer` objects are stored. It provides methods to add and retrieve customers. The `CustomerRegistry` is a singleton, meaning there is only one instance of it in the application.
74+
4575
```java
4676
public final class CustomerRegistry {
4777

4878
private static final CustomerRegistry instance = new CustomerRegistry();
4979

50-
public static CustomerRegistry getInstance() {
51-
return instance;
52-
}
53-
5480
private final Map<String, Customer> customerMap;
5581

5682
private CustomerRegistry() {
5783
customerMap = new ConcurrentHashMap<>();
5884
}
5985

86+
public static CustomerRegistry getInstance() {
87+
return instance;
88+
}
89+
6090
public Customer addCustomer(Customer customer) {
61-
return customerMap.put(customer.getId(), customer);
91+
return customerMap.put(customer.id(), customer);
6292
}
6393

6494
public Customer getCustomer(String id) {
@@ -68,17 +98,72 @@ public final class CustomerRegistry {
6898
}
6999
```
70100

101+
Finally, we have the `App` class. This class demonstrates how to use the `CustomerRegistry`. It creates two `Customer` objects, adds them to the `CustomerRegistry`, and then retrieves them.
102+
103+
```java
104+
public class App {
105+
106+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
107+
108+
public static void main(String[] args) {
109+
CustomerRegistry customerRegistry = CustomerRegistry.getInstance();
110+
var john = new Customer("1", "John");
111+
customerRegistry.addCustomer(john);
112+
var julia = new Customer("2", "Julia");
113+
customerRegistry.addCustomer(julia);
114+
115+
LOGGER.info("John {}", customerRegistry.getCustomer("1"));
116+
LOGGER.info("Julia {}", customerRegistry.getCustomer("2"));
117+
}
118+
119+
}
120+
```
121+
122+
In this example, the `CustomerRegistry` provides a global point of access to `Customer` objects. This allows us to manage these objects in a centralized way, promoting reuse and sharing, and facilitating decoupling between components.
123+
71124
## Class diagram
125+
72126
![Registry](./etc/registry.png)
73127

74128
## Applicability
75-
Use Registry pattern when
76129

77-
* client wants reference of some object, so client can lookup for that object in the object's registry.
130+
* When you need to manage a global collection of objects.
131+
* When you need to decouple the creation of objects from their usage.
132+
* When you need to ensure a controlled lifecycle for objects, such as services or resources.
133+
* When you want to avoid redundant creation of objects.
134+
135+
## Known Uses
136+
137+
* Managing database connections in an enterprise application.
138+
* Providing a central place to register and retrieve services or components in a modular application.
139+
* Creating a central configuration registry that various parts of an application can access.
78140

79141
## Consequences
80-
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.
142+
143+
Benefits:
144+
145+
* Centralizes object management, making the application easier to maintain.
146+
* Promotes reuse and sharing of objects, which can reduce memory footprint and initialization time.
147+
* Facilitates decoupling between components.
148+
149+
Trade-offs:
150+
151+
* Can become a bottleneck if not implemented efficiently.
152+
* May introduce a single point of failure if the registry is not designed to be fault-tolerant.
153+
* Increases complexity in managing the lifecycle of objects.
154+
155+
## Related Patterns
156+
157+
* [Singleton](https://java-design-patterns.com/patterns/singleton/): Often used in conjunction with the Registry to ensure there is a single instance of the Registry.
158+
* [Factory](https://java-design-patterns.com/patterns/factory/): Used to encapsulate the instantiation logic that might be needed when objects are retrieved from the Registry.
159+
* [Service Locator](https://java-design-patterns.com/patterns/service-locator/): A pattern that is similar in intent and structure, often used interchangeably with the Registry.
160+
* [Dependency Injection](https://java-design-patterns.com/patterns/dependency-injection/): Provides an alternative method for managing dependencies, which can sometimes replace the need for a Registry.
161+
* [Multiton](https://java-design-patterns.com/patterns/multiton/): Similar to the Registry in that it manages multiple instances, but does so based on keys, ensuring only one instance per key.
81162

82163
## Credits
83-
* https://www.martinfowler.com/eaaCatalog/registry.html
84-
* https://wiki.c2.com/?RegistryPattern
164+
165+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
166+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
167+
* [Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)
168+
* [Registry - Martin Fowler](https://www.martinfowler.com/eaaCatalog/registry.html)
169+
* [Registry pattern - wiki.c2.com](https://wiki.c2.com/?RegistryPattern)

registry/src/main/java/com/iluwatar/registry/CustomerRegistry.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@
2626

2727
import java.util.Map;
2828
import java.util.concurrent.ConcurrentHashMap;
29+
import lombok.Getter;
2930

3031
/**
3132
* CustomerRegistry class used to store/access {@link Customer} objects.
3233
*/
3334
public final class CustomerRegistry {
3435

36+
@Getter
3537
private static final CustomerRegistry instance = new CustomerRegistry();
3638

37-
public static CustomerRegistry getInstance() {
38-
return instance;
39-
}
40-
4139
private final Map<String, Customer> customerMap;
4240

4341
private CustomerRegistry() {

0 commit comments

Comments
 (0)