-
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat: add Immutable Object design pattern (#3448) #3485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prashantpiyush1111
wants to merge
4
commits into
iluwatar:master
Choose a base branch
from
prashantpiyush1111:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0fd6085
feat: add Immutable Object design pattern (#3448)
prashantpiyush1111 7cdf02b
fix: move ImmutableObject to correct package path
prashantpiyush1111 1c41504
test: add ImmutableObjectTest for coverage
prashantpiyush1111 26287aa
refactor: add equals, hashCode, null safety and tests
prashantpiyush1111 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Immutable Object Pattern | ||
|
|
||
| ## Intent | ||
| Ensure that an object's state cannot be modified | ||
| after it is created. | ||
|
|
||
| ## Explanation | ||
| An immutable object is one whose state cannot be | ||
| changed once created. All fields are final and set | ||
| only in the constructor. | ||
|
|
||
| ## When to use | ||
| - When shared objects must not be changed | ||
| - For thread-safe programming | ||
| - When you need predictable behavior | ||
|
|
||
| ## Example | ||
| HeroStats in a game — once created, stats remain fixed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?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.26.0-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>immutable</artifactId> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
50 changes: 50 additions & 0 deletions
50
immutable/src/main/java/com/iluwatar/immutable/ImmutableObject.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.iluwatar.immutable; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Immutable Object pattern. | ||
| * Once created, the object state cannot be changed. | ||
| */ | ||
| public final class ImmutableObject { | ||
|
|
||
| private final String name; | ||
| private final int age; | ||
| private final String email; | ||
|
|
||
| public ImmutableObject( | ||
| final String name, | ||
| final int age, | ||
| final String email) { | ||
| this.name = Objects.requireNonNull(name, "name cannot be null"); | ||
| this.age = age; | ||
| this.email = Objects.requireNonNull(email, "email cannot be null"); | ||
| } | ||
|
|
||
| public String getName() { return name; } | ||
| public int getAge() { return age; } | ||
| public String getEmail() { return email; } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ImmutableObject{" | ||
| + "name='" + name + '\'' | ||
| + ", age=" + age | ||
| + ", email='" + email + '\'' | ||
| + '}'; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (!(o instanceof ImmutableObject)) return false; | ||
| ImmutableObject that = (ImmutableObject) o; | ||
| return age == that.age | ||
| && Objects.equals(name, that.name) | ||
| && Objects.equals(email, that.email); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name, age, email); | ||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
immutable/src/test/java/com/iluwatar/immutable/ImmutableObjectTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.iluwatar.immutable; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class ImmutableObjectTest { | ||
|
|
||
| @Test | ||
| void testObjectCreation() { | ||
| ImmutableObject obj = | ||
| new ImmutableObject("Prashant", 22, "p@gmail.com"); | ||
|
|
||
| assertEquals("Prashant", obj.getName()); | ||
| assertEquals(22, obj.getAge()); | ||
| assertEquals("p@gmail.com", obj.getEmail()); | ||
| } | ||
|
|
||
| @Test | ||
| void testToString() { | ||
| ImmutableObject obj = | ||
| new ImmutableObject("Prashant", 22, "p@gmail.com"); | ||
|
|
||
| assertTrue(obj.toString().contains("Prashant")); | ||
| } | ||
|
|
||
| @Test | ||
| void testImmutability() { | ||
| ImmutableObject obj1 = | ||
| new ImmutableObject("A", 1, "a@gmail.com"); | ||
| ImmutableObject obj2 = | ||
| new ImmutableObject("A", 1, "a@gmail.com"); | ||
|
|
||
| assertNotSame(obj1, obj2); | ||
| } | ||
|
|
||
| @Test | ||
| void testEquality() { | ||
| ImmutableObject obj1 = | ||
| new ImmutableObject("A", 1, "a@gmail.com"); | ||
| ImmutableObject obj2 = | ||
| new ImmutableObject("A", 1, "a@gmail.com"); | ||
|
|
||
| assertEquals(obj1, obj2); | ||
| } | ||
|
|
||
| @Test | ||
| void testNullName() { | ||
| assertThrows(NullPointerException.class, () -> { | ||
| new ImmutableObject(null, 1, "a@gmail.com"); | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file declares a package path that does not align with typical Maven source layout. Maven/Java projects expect directory structure immutable/src/main/java/com/iluwatar/immutable/ImmutableObject.java for package com.iluwatar.immutable. Move the file to the proper path or adjust the package declaration to reflect the directory structure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit 7cdf02b.
File moved to correct path:
immutable/src/main/java/com/iluwatar/immutable/ImmutableObject.java