Compare commits
2 Commits
all-contri
...
all-contri
Author | SHA1 | Date | |
---|---|---|---|
be52efc49b | |||
e47a24643a |
@ -1314,15 +1314,6 @@
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "viveksb007",
|
||||
"name": "Vivek Singh",
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/12713808?v=4",
|
||||
"profile": "https://viveksb007.github.io",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 4,
|
||||
|
@ -10,7 +10,7 @@
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||
[](#contributors-)
|
||||
[](#contributors-)
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||
|
||||
# Introduction
|
||||
@ -285,9 +285,6 @@ This project is licensed under the terms of the MIT license.
|
||||
<td align="center"><a href="https://github.com/omk13"><img src="https://avatars0.githubusercontent.com/u/59054172?v=4" width="100px;" alt=""/><br /><sub><b>Omar Karazoun</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=omk13" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/jeff303"><img src="https://avatars0.githubusercontent.com/u/3521562?v=4" width="100px;" alt=""/><br /><sub><b>Jeff Evans</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=jeff303" title="Code">💻</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://viveksb007.github.io"><img src="https://avatars1.githubusercontent.com/u/12713808?v=4" width="100px;" alt=""/><br /><sub><b>Vivek Singh</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=viveksb007" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<!-- markdownlint-enable -->
|
||||
|
1
pom.xml
1
pom.xml
@ -195,7 +195,6 @@
|
||||
<module>strangler</module>
|
||||
<module>arrange-act-assert</module>
|
||||
<module>transaction-script</module>
|
||||
<module>registry</module>
|
||||
<module>filterer</module>
|
||||
<module>factory</module>
|
||||
<module>separated-interface</module>
|
||||
|
@ -1,86 +0,0 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Registry
|
||||
folder: registry
|
||||
permalink: /patterns/registry/
|
||||
categories: Creational
|
||||
tags:
|
||||
- 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
|
||||
|
||||
```java
|
||||
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`
|
||||
```java
|
||||
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
|
||||

|
||||
|
||||
## 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
|
||||
* https://www.martinfowler.com/eaaCatalog/registry.html
|
||||
* https://wiki.c2.com/?RegistryPattern
|
Binary file not shown.
Before Width: | Height: | Size: 16 KiB |
@ -1,21 +0,0 @@
|
||||
@startuml
|
||||
package com.iluwatar.registry {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class Customer {
|
||||
- id : String
|
||||
- name : String
|
||||
+ getId() : String
|
||||
+ getName() : String
|
||||
+ toString() : String
|
||||
}
|
||||
class CustomerRegistry {
|
||||
+ addCustomer(customer : Customer)
|
||||
+ getCustomer(id : String)
|
||||
}
|
||||
}
|
||||
Customer --> "-addCustomer" CustomerRegistry
|
||||
@enduml
|
@ -1,46 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>registry</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.iluwatar.registry.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -1,27 +0,0 @@
|
||||
package com.iluwatar.registry;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
CustomerRegistry customerRegistry = CustomerRegistry.getInstance();
|
||||
var john = new Customer("1", "John");
|
||||
customerRegistry.addCustomer(john);
|
||||
|
||||
var julia = new Customer("2", "Julia");
|
||||
customerRegistry.addCustomer(julia);
|
||||
|
||||
LOGGER.info("John {}", customerRegistry.getCustomer("1"));
|
||||
LOGGER.info("Julia {}", customerRegistry.getCustomer("2"));
|
||||
}
|
||||
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package com.iluwatar.registry;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer{"
|
||||
+ "id='" + id + '\''
|
||||
+ ", name='" + name + '\''
|
||||
+ '}';
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package com.iluwatar.registry;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package com.iluwatar.registry;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class CustomerRegistryTest {
|
||||
|
||||
private static CustomerRegistry customerRegistry;
|
||||
|
||||
@BeforeAll
|
||||
public static void setUp() {
|
||||
customerRegistry = CustomerRegistry.getInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBeAbleToAddAndQueryCustomerObjectFromRegistry() {
|
||||
Customer john = new Customer("1", "john");
|
||||
Customer julia = new Customer("2", "julia");
|
||||
|
||||
customerRegistry.addCustomer(john);
|
||||
customerRegistry.addCustomer(julia);
|
||||
|
||||
Customer customerWithId1 = customerRegistry.getCustomer("1");
|
||||
assertNotNull(customerWithId1);
|
||||
assertEquals("1", customerWithId1.getId());
|
||||
assertEquals("john", customerWithId1.getName());
|
||||
|
||||
Customer customerWithId2 = customerRegistry.getCustomer("2");
|
||||
assertNotNull(customerWithId2);
|
||||
assertEquals("2", customerWithId2.getId());
|
||||
assertEquals("julia", customerWithId2.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullWhenQueriedCustomerIsNotInRegistry() {
|
||||
Customer customerWithId5 = customerRegistry.getCustomer("5");
|
||||
assertNull(customerWithId5);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user