Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit14c4710

Browse files
viveksb007ohbus
andauthored
Implemented Registry pattern (iluwatar#1543)
*iluwatar#1310 Implemented registry pattern* fixed parent pom version* added empty line in registry.urm.pumlCo-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
1 parent428cbc1 commit14c4710

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+
publicclassCustomer {
26+
27+
privatefinalString id;
28+
privatefinalString name;
29+
30+
publicCustomer(Stringid,Stringname) {
31+
this.id= id;
32+
this.name= name;
33+
}
34+
35+
publicStringgetId() {
36+
return id;
37+
}
38+
39+
publicStringgetName() {
40+
return name;
41+
}
42+
43+
}
44+
```
45+
46+
This registry of the`Customer` objects is`CustomerRegistry`
47+
```java
48+
publicfinalclassCustomerRegistry {
49+
50+
privatestaticfinalCustomerRegistry instance=newCustomerRegistry();
51+
52+
publicstaticCustomerRegistrygetInstance() {
53+
return instance;
54+
}
55+
56+
privatefinalMap<String,Customer> customerMap;
57+
58+
privateCustomerRegistry() {
59+
customerMap=newConcurrentHashMap<>();
60+
}
61+
62+
publicCustomeraddCustomer(Customercustomer) {
63+
return customerMap.put(customer.getId(), customer);
64+
}
65+
66+
publicCustomergetCustomer(Stringid) {
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+
packagecom.iluwatar.registry {
3+
classApp {
4+
-LOGGER : Logger {static}
5+
+App()
6+
+main(args : String[]) {static}
7+
}
8+
classCustomer {
9+
-id :String
10+
-name :String
11+
+getId() :String
12+
+getName() :String
13+
+toString() :String
14+
}
15+
classCustomerRegistry {
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+
<projectxmlns="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+
packagecom.iluwatar.registry;
2+
3+
importorg.slf4j.Logger;
4+
importorg.slf4j.LoggerFactory;
5+
6+
publicclassApp {
7+
8+
privatestaticfinalLoggerLOGGER =LoggerFactory.getLogger(App.class);
9+
10+
/**
11+
* Program entry point.
12+
*
13+
* @param args command line args
14+
*/
15+
publicstaticvoidmain(String[]args) {
16+
CustomerRegistrycustomerRegistry =CustomerRegistry.getInstance();
17+
varjohn =newCustomer("1","John");
18+
customerRegistry.addCustomer(john);
19+
20+
varjulia =newCustomer("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+
packagecom.iluwatar.registry;
2+
3+
publicclassCustomer {
4+
5+
privatefinalStringid;
6+
privatefinalStringname;
7+
8+
publicCustomer(Stringid,Stringname) {
9+
this.id =id;
10+
this.name =name;
11+
}
12+
13+
publicStringgetId() {
14+
returnid;
15+
}
16+
17+
publicStringgetName() {
18+
returnname;
19+
}
20+
21+
@Override
22+
publicStringtoString() {
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+
packagecom.iluwatar.registry;
2+
3+
importjava.util.Map;
4+
importjava.util.concurrent.ConcurrentHashMap;
5+
6+
publicfinalclassCustomerRegistry {
7+
8+
privatestaticfinalCustomerRegistryinstance =newCustomerRegistry();
9+
10+
publicstaticCustomerRegistrygetInstance() {
11+
returninstance;
12+
}
13+
14+
privatefinalMap<String,Customer>customerMap;
15+
16+
privateCustomerRegistry() {
17+
customerMap =newConcurrentHashMap<>();
18+
}
19+
20+
publicCustomeraddCustomer(Customercustomer) {
21+
returncustomerMap.put(customer.getId(),customer);
22+
}
23+
24+
publicCustomergetCustomer(Stringid) {
25+
returncustomerMap.get(id);
26+
}
27+
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packagecom.iluwatar.registry;
2+
3+
importorg.junit.jupiter.api.BeforeAll;
4+
importorg.junit.jupiter.api.Test;
5+
6+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
7+
importstaticorg.junit.jupiter.api.Assertions.assertNotNull;
8+
importstaticorg.junit.jupiter.api.Assertions.assertNull;
9+
10+
publicclassCustomerRegistryTest {
11+
12+
privatestaticCustomerRegistrycustomerRegistry;
13+
14+
@BeforeAll
15+
publicstaticvoidsetUp() {
16+
customerRegistry =CustomerRegistry.getInstance();
17+
}
18+
19+
@Test
20+
publicvoidshouldBeAbleToAddAndQueryCustomerObjectFromRegistry() {
21+
Customerjohn =newCustomer("1","john");
22+
Customerjulia =newCustomer("2","julia");
23+
24+
customerRegistry.addCustomer(john);
25+
customerRegistry.addCustomer(julia);
26+
27+
CustomercustomerWithId1 =customerRegistry.getCustomer("1");
28+
assertNotNull(customerWithId1);
29+
assertEquals("1",customerWithId1.getId());
30+
assertEquals("john",customerWithId1.getName());
31+
32+
CustomercustomerWithId2 =customerRegistry.getCustomer("2");
33+
assertNotNull(customerWithId2);
34+
assertEquals("2",customerWithId2.getId());
35+
assertEquals("julia",customerWithId2.getName());
36+
}
37+
38+
@Test
39+
publicvoidshouldReturnNullWhenQueriedCustomerIsNotInRegistry() {
40+
CustomercustomerWithId5 =customerRegistry.getCustomer("5");
41+
assertNull(customerWithId5);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp