Skip to content

refactor: apply Factory Enum Pattern for cleaner object instantiation#3523

Open
prashantpiyush1111 wants to merge 5 commits into
iluwatar:masterfrom
prashantpiyush1111:fix/factory-clean
Open

refactor: apply Factory Enum Pattern for cleaner object instantiation#3523
prashantpiyush1111 wants to merge 5 commits into
iluwatar:masterfrom
prashantpiyush1111:fix/factory-clean

Conversation

@prashantpiyush1111

Copy link
Copy Markdown

Closes #3301

Applied Factory Enum Pattern to CoinType enum:

  • Added getInstance() method for singleton instance per coin type
  • Removed dependency on CoinFactory in App.java
  • Updated tests to use getInstance() directly

@github-actions

github-actions Bot commented Jun 14, 2026

Copy link
Copy Markdown

PR Summary

Closes #3301. Applied the Factory Enum Pattern to the CoinType enum by introducing a per-type singleton via getInstance(); removed CoinFactory dependency from App.java; updated tests to instantiate coins via CoinType.getInstance() and added tests to verify singleton behavior.

Changes

File Summary
factory/src/main/java/com/iluwatar/factory/App.java Replaced CoinFactory usage with per-type singleton access: CoinType.COPPER.getInstance() and CoinType.GOLD.getInstance() in main; preserves coin description logs.
factory/src/main/java/com/iluwatar/factory/CoinType.java Introduced lazy singleton per coin type via getInstance(); added private instance field and getter; removed CoinFactory dependency in favor of enum-provided instances.
factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java Updated tests to use CoinType.getInstance(); added tests for copper instance and singleton equality.

autogenerated by presubmit.ai

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
  • 7c3a3a0: refactor: apply Factory Enum Pattern for cleaner object instantiation
Files Processed (3)
  • factory/src/main/java/com/iluwatar/factory/App.java (1 hunk)
  • factory/src/main/java/com/iluwatar/factory/CoinType.java (2 hunks)
  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (3)
  • factory/src/main/java/com/iluwatar/factory/App.java [43-44]

    possible bug: "Make singleton per coin type thread-safe"

  • factory/src/main/java/com/iluwatar/factory/CoinType.java [41-50]

    possible bug: "Thread-safety of per-type singleton"

  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [33-50]

    testing: "Tests for CoinType#getInstance() behavior"

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
  • 106b065: style: fix formatting in CoinFactoryTest
Files Processed (1)
  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (3)
  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [33-37]

    readability: "Clarify singleton non-null contract in copper coin test."

  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [39-43]

    readability: "Improve parity and clarity for gold coin singleton test."

  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [45-49]

    readability: "Consolidate singleton consistency checks for both coin types."

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
  • 402abb4: refactor(factory): apply enum-based factory pattern
Files Processed (1)
  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (3)
  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [33-37]

    readability: "Tighten type assertion for Copper coin instance"

  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [39-43]

    readability: "Tighten type assertion for Gold coin instance"

  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [45-49]

    readability: "Confirm singleton semantics across both types"

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
  • 1e2350a: style(factory): fix test indentation
Files Processed (1)
  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (3)
  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [33-37]

    best_practice: "Singleton per-coin instance expectations"

  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [39-43]

    best_practice: "Singleton per-coin instance expectations"

  • factory/src/test/java/com/iluwatar/factory/CoinFactoryTest.java [45-49]

    best_practice: "Ensure distinct singletons per coin type"

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
  • 569afc8: style(factory): add trailing newline
Files Processed (1)
  • factory/src/main/java/com/iluwatar/factory/CoinType.java (2 hunks)
Actionable Comments (1)
  • factory/src/main/java/com/iluwatar/factory/CoinType.java [45-50]

    possible bug: "Thread-safety concern in lazy initialization."

Skipped Comments (1)
  • factory/src/main/java/com/iluwatar/factory/CoinType.java [41-43]

    readability: "Per-type singleton field lacks visibility guarantees; ensure proper memory semantics."

Comment on lines +45 to +50
public Coin getInstance() {
if (instance == null) {
instance = constructor.get();
}
return instance;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getInstance() implements lazy initialization without synchronization, which is not thread-safe. In multi-threaded scenarios, two threads could observe instance == null and both create a Coin, potentially leaking one and returning a non-singleton. Consider a thread-safe lazy initialization strategy (e.g., double-checked locking with a volatile field, or initialize at enum construction).

@codecov

codecov Bot commented Jun 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.28%. Comparing base (fcdf639) to head (569afc8).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3523      +/-   ##
============================================
- Coverage     83.28%   83.28%   -0.01%     
- Complexity     4023     4024       +1     
============================================
  Files          1060     1060              
  Lines         14246    14249       +3     
  Branches        686      686              
============================================
+ Hits          11865    11867       +2     
- Misses         2094     2095       +1     
  Partials        287      287              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor to Factory Enum Pattern for Cleaner Object Instantiation

1 participant