Skip to content
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions Identity_Field/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
title: "Feature Toggle Pattern in Java: Managing Features in Production Seamlessly"
shortTitle: Feature Toggle
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."
category: Behavioral
language: en
tag:
- Decoupling
- Extensibility
- Feature management
- Scalability
---

## Title: Identity field pattern in Java
## Short Title: Identity Field

> to allow having a unique key for each instance to ensure consistency by always referring to objects using their unique identifiers.
## Detailed Explanation of Identity field Pattern with Real-World Examples
## Category : Creational
## lang : en
Real-world Example

> 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
Use the Identity Field pattern to assign each book a unique id (e.g., ISBN or database-generated ID).
Persist book details in a database with id as the primary key.
In plain words

Wikipedia says

> A primary key is a designated attribute (column) that can reliably identify and distinguish between each individual record in a table.

## Programmatic Example of Identity Field Pattern in Java


Key Components:

1. `DomainObject`: This class uses properties to generate IDs using incremental method were every instance get the incremented value of the previous one.

```java
package com.iluwater.fieild;


import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;

@MappedSuperclass
public abstract class DomainObject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // automatically generate unique values for primary key columns
//strategy = GenerationType.IDENTITY generate the primary key value by the database itself using the auto-increment column option
private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}

```

## When to Use the Identity field Pattern in Java

Use the Identity field Pattern in Java when:

* ORMs like Hibernate and JPA rely heavily on the Identity Field Pattern to map objects to rows in a database.
* Microservices with Distributed Systems
* Content Management Systems (CMS)
* Financial Systems
* Healthcare Management Systems

## Real-World Applications of Identity Field pattern in Java

* Amazon's inventory system uses unique productId values for inventory tracking across multiple warehouses globally.
* An airline assigns unique ticketId values to each booking, which are linked to passenger details and flight schedules.
* A hospital management system assigns a unique patientId to each patient to link their medical records securely.

## Benefits and Trade-offs of Identity Field pattern

Benefits:

* This pattern is essential when the uniqueness of certain attributes is required to maintain data integrity, avoid conflicts, and support efficient lookups.

Trade-offs:

* Storage Overhead.
* Complications with Composite IDs
* Overhead of ID Generation if not incremental or includes characters

## Related Java Design Patterns

* Singleton Pattern
* Factory Pattern
* Repository

## References and Credits

* https://www.geeksforgeeks.org/hibernate-generatedvalue-annotation-in-jpa/
28 changes: 28 additions & 0 deletions Identity_Field/etc/Identity.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@startuml
abstract class DomainObject {
+ id: Long
+ getId(): Long
+ setId(id: Long): void
}

class Book {
- title: String
- author: String
+ getTitle(): String
+ setTitle(title: String): void
+ getAuthor(): String
+ setAuthor(author: String): void
}

class Client {
- name: String
- Email: String
+ getName(): String
+ setName(name: String): void
+ getEmail(): String
+ setEmail(email: String): void
}

DomainObject <|-- Book
DomainObject <|-- Client
@enduml
83 changes: 83 additions & 0 deletions Identity_Field/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
The MIT License
Copyright © 2014-2022 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<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.26.0-SNAPSHOT</version>
</parent>
<artifactId>Identity_Field</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.iluwater.fieild.Controller;

import com.iluwater.fieild.Model.Book;
import com.iluwater.fieild.Services.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
public class BookController {
private final BookService bookService;

@Autowired
public BookController(BookService bookService) {
this.bookService = bookService;
}

// Create a new book
@PostMapping
public Book createBook(@RequestBody String title, String Author) {
return bookService.createBook(title,Author);
}

// Get a book by ID
@GetMapping("/{id}")
public Book getBook(@PathVariable Long id) {
return bookService.getBookById(id);
}

}
44 changes: 44 additions & 0 deletions Identity_Field/src/main/java/com/iluwater/fieild/Model/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwater.fieild.Model;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;

@Setter
@Getter
@Entity
public class Book extends DomainObject {
private String title;
private String author;


public Book(String title, String author) {
this.title = title;
this.author = author;
}

}
48 changes: 48 additions & 0 deletions Identity_Field/src/main/java/com/iluwater/fieild/Model/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwater.fieild.Model;
import java.util.regex.Pattern;

import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;

@Setter
@Getter
@Entity
public class Client extends DomainObject {
private static final String EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
private String name;
private String Email;

public Client(String name, String Email) {
Pattern pattern = Pattern.compile(EMAIL_REGEX);
this.name = name;
if (pattern.matcher(Email).matches()) {
this.Email = Email;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwater.fieild.Model;
/*
* The DomainObject class provides a base for domain entities
* that require unique identification within the application.
*
* <p>All child classes inherit the unique identifier field
* and associated functionality.</p>
*/
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@MappedSuperclass
@Entity
public abstract class DomainObject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // automatically generate unique values for primary key columns
private Long id;
//strategy = GenerationType.IDENTITY generate the primary key value by the database itself using the auto-increment column option


}
Loading
Loading