Skip to content

Commit 1c5be7a

Browse files
Identity Field Pattern
1 parent b375919 commit 1c5be7a

File tree

9 files changed

+345
-0
lines changed

9 files changed

+345
-0
lines changed

Identity_Field/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: "Feature Toggle Pattern in Java: Managing Features in Production Seamlessly"
3+
shortTitle: Feature Toggle
4+
description: "Learn how to implement the Feature Toggle design pattern in Java. This guide covers dynamic feature management, benefits, use cases, and practical examples to help you enhance your software development process."
5+
category: Behavioral
6+
language: en
7+
tag:
8+
- Decoupling
9+
- Extensibility
10+
- Feature management
11+
- Scalability
12+
---
13+
14+
## Title: Identity field pattern in Java
15+
## Short Title: Identity Field
16+
17+
> to allow having a unique key for each instance to ensure consistency by always referring to objects using their unique identifiers.
18+
## Detailed Explanation of Identity field Pattern with Real-World Examples
19+
## Category : Creational
20+
## lang : en
21+
Real-world Example
22+
23+
> A real-world example of the Identity filed pattern is Library Management System. A library tracks books borrowed by users. Each book needs a unique identity to distinguish it, even if multiple copies of the same title exist. to solve this
24+
Use the Identity Field pattern to assign each book a unique id (e.g., ISBN or database-generated ID).
25+
Persist book details in a database with id as the primary key.
26+
In plain words
27+
28+
Wikipedia says
29+
30+
> A primary key is a designated attribute (column) that can reliably identify and distinguish between each individual record in a table.
31+
32+
## Programmatic Example of Identity Field Pattern in Java
33+
34+
35+
Key Components:
36+
37+
1. `DomainObject`: This class uses properties to generate IDs using incremental method were every instance get the incremented value of the previous one.
38+
39+
```java
40+
package com.iluwater.fieild;
41+
42+
43+
import jakarta.persistence.Id;
44+
import jakarta.persistence.GeneratedValue;
45+
import jakarta.persistence.GenerationType;
46+
47+
@MappedSuperclass
48+
public abstract class DomainObject {
49+
@Id
50+
@GeneratedValue(strategy = GenerationType.IDENTITY) // automatically generate unique values for primary key columns
51+
//strategy = GenerationType.IDENTITY generate the primary key value by the database itself using the auto-increment column option
52+
private Long id;
53+
54+
public Long getId() {
55+
return id;
56+
}
57+
58+
public void setId(Long id) {
59+
this.id = id;
60+
}
61+
}
62+
63+
```
64+
65+
## When to Use the Identity field Pattern in Java
66+
67+
Use the Identity field Pattern in Java when:
68+
69+
* ORMs like Hibernate and JPA rely heavily on the Identity Field Pattern to map objects to rows in a database.
70+
* Microservices with Distributed Systems
71+
* Content Management Systems (CMS)
72+
* Financial Systems
73+
* Healthcare Management Systems
74+
75+
## Real-World Applications of Identity Field pattern in Java
76+
77+
* Amazon's inventory system uses unique productId values for inventory tracking across multiple warehouses globally.
78+
* An airline assigns unique ticketId values to each booking, which are linked to passenger details and flight schedules.
79+
* A hospital management system assigns a unique patientId to each patient to link their medical records securely.
80+
81+
## Benefits and Trade-offs of Identity Field pattern
82+
83+
Benefits:
84+
85+
* This pattern is essential when the uniqueness of certain attributes is required to maintain data integrity, avoid conflicts, and support efficient lookups.
86+
87+
Trade-offs:
88+
89+
* Storage Overhead.
90+
* Complications with Composite IDs
91+
* Overhead of ID Generation if not incremental or includes characters
92+
93+
## Related Java Design Patterns
94+
95+
* Singleton Pattern
96+
* Factory Pattern
97+
* Repository
98+
99+
## References and Credits
100+
101+
* https://www.geeksforgeeks.org/hibernate-generatedvalue-annotation-in-jpa/

Identity_Field/etc/Identity.puml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@startuml
2+
abstract class DomainObject {
3+
+ id: Long
4+
+ getId(): Long
5+
+ setId(id: Long): void
6+
}
7+
8+
class Book {
9+
- title: String
10+
- author: String
11+
+ getTitle(): String
12+
+ setTitle(title: String): void
13+
+ getAuthor(): String
14+
+ setAuthor(author: String): void
15+
}
16+
17+
class Client {
18+
- name: String
19+
- Email: String
20+
+ getName(): String
21+
+ setName(name: String): void
22+
+ getEmail(): String
23+
+ setEmail(email: String): void
24+
}
25+
26+
DomainObject <|-- Book
27+
DomainObject <|-- Client
28+
@enduml

Identity_Field/pom.xml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
4+
The MIT License
5+
Copyright © 2014-2022 Ilkka Seppälä
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0"
23+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
24+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
25+
<modelVersion>4.0.0</modelVersion>
26+
<parent>
27+
<groupId>com.iluwatar</groupId>
28+
<artifactId>java-design-patterns</artifactId>
29+
<version>1.26.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>Identity_Field</artifactId>
33+
34+
<properties>
35+
<maven.compiler.source>17</maven.compiler.source>
36+
<maven.compiler.target>17</maven.compiler.target>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
</properties>
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.junit.jupiter</groupId>
42+
<artifactId>junit-jupiter-api</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>jakarta.persistence</groupId>
47+
<artifactId>jakarta.persistence-api</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.iluwatar</groupId>
51+
<artifactId>update-method</artifactId>
52+
<version>1.26.0-SNAPSHOT</version>
53+
<scope>compile</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.fasterxml.jackson.core</groupId>
57+
<artifactId>jackson-annotations</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>jakarta.persistence</groupId>
61+
<artifactId>jakarta.persistence-api</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.hibernate</groupId>
65+
<artifactId>hibernate-core</artifactId>
66+
</dependency>
67+
<dependency>
68+
<groupId>jakarta.persistence</groupId>
69+
<artifactId>jakarta.persistence-api</artifactId>
70+
</dependency>
71+
<dependency>
72+
<groupId>jakarta.persistence</groupId>
73+
<artifactId>jakarta.persistence-api</artifactId>
74+
</dependency>
75+
<dependency>
76+
<groupId>jakarta.persistence</groupId>
77+
<artifactId>jakarta.persistence-api</artifactId>
78+
</dependency>
79+
<dependency>
80+
<groupId>jakarta.persistence</groupId>
81+
<artifactId>jakarta.persistence-api</artifactId>
82+
</dependency>
83+
<dependency>
84+
<groupId>jakarta.persistence</groupId>
85+
<artifactId>jakarta.persistence-api</artifactId>
86+
</dependency>
87+
88+
</dependencies>
89+
90+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.iluwater.fieild;
2+
import jakarta.persistence.Entity;
3+
4+
@Entity
5+
public class Book extends DomainObject {
6+
private String title;
7+
private String author;
8+
9+
10+
public Book(String title, String author) {
11+
this.title = title;
12+
this.author = author;
13+
}
14+
15+
public String getTitle() {
16+
return title;
17+
}
18+
19+
public void setTitle(String title) {
20+
this.title = title;
21+
}
22+
23+
public String getAuthor() {
24+
return author;
25+
}
26+
27+
public void setAuthor(String author) {
28+
this.author = author;
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.iluwater.fieild;
2+
3+
public class Client extends DomainObject{
4+
private String name;
5+
private String Email;
6+
7+
8+
public Client(String name,String Email) {
9+
this.name = name;
10+
this.Email=Email;
11+
}
12+
13+
public void setEmail(String email) {
14+
Email = email;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public String getEmail() {
26+
return Email;
27+
}
28+
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwater.fieild;
2+
3+
4+
import jakarta.persistence.Id;
5+
import jakarta.persistence.GeneratedValue;
6+
import jakarta.persistence.GenerationType;
7+
8+
@MappedSuperclass
9+
public abstract class DomainObject {
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.IDENTITY) // automatically generate unique values for primary key columns
12+
//strategy = GenerationType.IDENTITY generate the primary key value by the database itself using the auto-increment column option
13+
private Long id;
14+
15+
public Long getId() {
16+
return id;
17+
}
18+
19+
public void setId(Long id) {
20+
this.id = id;
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwater.fieild;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class BookTest {
8+
@Test
9+
void checkIdNotNull()
10+
{
11+
Book book= new Book("Design patterns","someone");
12+
assertNotNull(book.getId());
13+
}
14+
void checkTwoIdsNotEqual()
15+
{
16+
Book book= new Book("Design patterns","someone");
17+
Book book2= new Book("Head first","someone");
18+
assertNotEquals(book.getId(),book2.getId());
19+
}
20+
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwater.fieild;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
class ClientTest {
6+
void checkIdNotNull()
7+
{
8+
Client client= new Client("John","[email protected]");
9+
assertNotNull(client.getId());
10+
}
11+
void checkTwoIdsNotEqual()
12+
{
13+
Client client= new Client("John","[email protected]");
14+
Client client2= new Client("Sara","[email protected]");
15+
assertNotEquals(client.getId(),client2.getId());
16+
}
17+
void checkEmails()
18+
{
19+
Client client= new Client("John","[email protected]");
20+
Client client2= new Client("Sara","[email protected]");
21+
assertNotEquals(client.getEmail(),client2.getEmail());
22+
}
23+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
<module>function-composition</module>
219219
<module>microservices-distributed-tracing</module>
220220
<module>microservices-idempotent-consumer</module>
221+
<module>Identity_Field</module>
221222
</modules>
222223
<repositories>
223224
<repository>

0 commit comments

Comments
 (0)