Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 14c4710

Browse files
viveksb007ohbus
andauthored
Implemented Registry pattern (iluwatar#1543)
* iluwatar#1310 Implemented registry pattern * fixed parent pom version * added empty line in registry.urm.puml Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
1 parent 428cbc1 commit 14c4710

File tree

9 files changed

+281
-0
lines changed

9 files changed

+281
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
<module>strangler</module>
196196
<module>arrange-act-assert</module>
197197
<module>transaction-script</module>
198+
<module>registry</module>
198199
<module>filterer</module>
199200
<module>factory</module>
200201
<module>separated-interface</module>

registry/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
layout: pattern
3+
title: Registry
4+
folder: registry
5+
permalink: /patterns/registry/
6+
categories: Creational
7+
tags:
8+
- Instantiation
9+
---
10+
11+
## Intent
12+
Stores the objects of a single class and provide a global point of access to them.
13+
Similar to Multiton pattern, only difference is that in a registry there is no restriction on the number of objects.
14+
15+
## Explanation
16+
17+
In Plain Words
18+
19+
> Registry is a well-known object that other objects can use to find common objects and services.
20+
21+
**Programmatic Example**
22+
Below is a `Customer` Class
23+
24+
```java
25+
public class Customer {
26+
27+
private final String id;
28+
private final String name;
29+
30+
public Customer(String id, String name) {
31+
this.id = id;
32+
this.name = name;
33+
}
34+
35+
public String getId() {
36+
return id;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
}
44+
```
45+
46+
This registry of the `Customer` objects is `CustomerRegistry`
47+
```java
48+
public final class CustomerRegistry {
49+
50+
private static final CustomerRegistry instance = new CustomerRegistry();
51+
52+
public static CustomerRegistry getInstance() {
53+
return instance;
54+
}
55+
56+
private final Map<String, Customer> customerMap;
57+
58+
private CustomerRegistry() {
59+
customerMap = new ConcurrentHashMap<>();
60+
}
61+
62+
public Customer addCustomer(Customer customer) {
63+
return customerMap.put(customer.getId(), customer);
64+
}
65+
66+
public Customer getCustomer(String id) {
67+
return customerMap.get(id);
68+
}
69+
70+
}
71+
```
72+
73+
## Class diagram
74+
![Registry](./etc/registry.png)
75+
76+
## Applicability
77+
Use Registry pattern when
78+
79+
* client wants reference of some object, so client can lookup for that object in the object's registry.
80+
81+
## Consequences
82+
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.
83+
84+
## Credits
85+
* https://www.martinfowler.com/eaaCatalog/registry.html
86+
* https://wiki.c2.com/?RegistryPattern

registry/etc/registry.png

16.5 KB
Loading

registry/etc/registry.urm.puml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@startuml
2+
package com.iluwatar.registry {
3+
class App {
4+
- LOGGER : Logger {static}
5+
+ App()
6+
+ main(args : String[]) {static}
7+
}
8+
class Customer {
9+
- id : String
10+
- name : String
11+
+ getId() : String
12+
+ getName() : String
13+
+ toString() : String
14+
}
15+
class CustomerRegistry {
16+
+ addCustomer(customer : Customer)
17+
+ getCustomer(id : String)
18+
}
19+
}
20+
Customer --> "-addCustomer" CustomerRegistry
21+
@enduml

registry/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.iluwatar</groupId>
9+
<artifactId>java-design-patterns</artifactId>
10+
<version>1.24.0-SNAPSHOT</version>
11+
</parent>
12+
<artifactId>registry</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.junit.jupiter</groupId>
17+
<artifactId>junit-jupiter-engine</artifactId>
18+
<scope>test</scope>
19+
</dependency>
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-assembly-plugin</artifactId>
31+
<executions>
32+
<execution>
33+
<configuration>
34+
<archive>
35+
<manifest>
36+
<mainClass>com.iluwatar.registry.App</mainClass>
37+
</manifest>
38+
</archive>
39+
</configuration>
40+
</execution>
41+
</executions>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.iluwatar.registry;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class App {
7+
8+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
9+
10+
/**
11+
* Program entry point.
12+
*
13+
* @param args command line args
14+
*/
15+
public static void main(String[] args) {
16+
CustomerRegistry customerRegistry = CustomerRegistry.getInstance();
17+
var john = new Customer("1", "John");
18+
customerRegistry.addCustomer(john);
19+
20+
var julia = new Customer("2", "Julia");
21+
customerRegistry.addCustomer(julia);
22+
23+
LOGGER.info("John {}", customerRegistry.getCustomer("1"));
24+
LOGGER.info("Julia {}", customerRegistry.getCustomer("2"));
25+
}
26+
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar.registry;
2+
3+
public class Customer {
4+
5+
private final String id;
6+
private final String name;
7+
8+
public Customer(String id, String name) {
9+
this.id = id;
10+
this.name = name;
11+
}
12+
13+
public String getId() {
14+
return id;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "Customer{"
24+
+ "id='" + id + '\''
25+
+ ", name='" + name + '\''
26+
+ '}';
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar.registry;
2+
3+
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
6+
public final class CustomerRegistry {
7+
8+
private static final CustomerRegistry instance = new CustomerRegistry();
9+
10+
public static CustomerRegistry getInstance() {
11+
return instance;
12+
}
13+
14+
private final Map<String, Customer> customerMap;
15+
16+
private CustomerRegistry() {
17+
customerMap = new ConcurrentHashMap<>();
18+
}
19+
20+
public Customer addCustomer(Customer customer) {
21+
return customerMap.put(customer.getId(), customer);
22+
}
23+
24+
public Customer getCustomer(String id) {
25+
return customerMap.get(id);
26+
}
27+
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.iluwatar.registry;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertNotNull;
8+
import static org.junit.jupiter.api.Assertions.assertNull;
9+
10+
public class CustomerRegistryTest {
11+
12+
private static CustomerRegistry customerRegistry;
13+
14+
@BeforeAll
15+
public static void setUp() {
16+
customerRegistry = CustomerRegistry.getInstance();
17+
}
18+
19+
@Test
20+
public void shouldBeAbleToAddAndQueryCustomerObjectFromRegistry() {
21+
Customer john = new Customer("1", "john");
22+
Customer julia = new Customer("2", "julia");
23+
24+
customerRegistry.addCustomer(john);
25+
customerRegistry.addCustomer(julia);
26+
27+
Customer customerWithId1 = customerRegistry.getCustomer("1");
28+
assertNotNull(customerWithId1);
29+
assertEquals("1", customerWithId1.getId());
30+
assertEquals("john", customerWithId1.getName());
31+
32+
Customer customerWithId2 = customerRegistry.getCustomer("2");
33+
assertNotNull(customerWithId2);
34+
assertEquals("2", customerWithId2.getId());
35+
assertEquals("julia", customerWithId2.getName());
36+
}
37+
38+
@Test
39+
public void shouldReturnNullWhenQueriedCustomerIsNotInRegistry() {
40+
Customer customerWithId5 = customerRegistry.getCustomer("5");
41+
assertNull(customerWithId5);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)