Skip to content

Refactor: improve encapsulation in FakeSequence by privatizing fields and adding protected getters#1779

Closed
akanksharaut2662-rgb wants to merge 1 commit intodatafaker-net:mainfrom
akanksharaut2662-rgb:refactor/fakesequence-encapsulation-pullup
Closed

Refactor: improve encapsulation in FakeSequence by privatizing fields and adding protected getters#1779
akanksharaut2662-rgb wants to merge 1 commit intodatafaker-net:mainfrom
akanksharaut2662-rgb:refactor/fakesequence-encapsulation-pullup

Conversation

@akanksharaut2662-rgb
Copy link
Copy Markdown

Summary

This PR improves encapsulation in FakeSequence by restricting direct field access and introducing protected getter methods for subclass usage.

Changes Made

  • FakeSequence.java - Improve Encapsulation
  • Refactored field visibility and access patterns in FakeSequence to remove direct subclass access to internal state.

Before:

All fields in FakeSequence were declared as protected, allowing subclasses to directly access the internal state. This creates a Deficient Encapsulation smell because protected fields implicitly couple subclasses to the parent’s internal representation, making future refactoring more difficult.

In practice, no subclass required direct access to these fields since all values are provided through the constructor.

After:

Applied Pull-up Method style refactoring to improve encapsulation:

  • Changed all fields in FakeSequence from protected to private
  • Added protected getter methods in the abstract parent class
  • Updated FakeStream and FakeCollection to use these getters instead of direct field access

Testing

  • No behavior change
  • All existing tests pass

Notes

This refactoring improves maintainability by enforcing proper encapsulation while preserving the existing class hierarchy and behavior.

@what-the-diff
Copy link
Copy Markdown

what-the-diff bot commented Mar 14, 2026

PR Summary

  • Improvements to FakeCollection Class
    The way the size of an item in FakeCollection class (a type of representation for data) is now determined more accurately and better managed with improved methods, contributing to greater reliability in data manipulation.

  • Updates to the FakeSequence Class

    • Security Enhancement: The level of access to member variables has been strengthened, changing from 'protected' (shared within its own class and subclasses) to private (restricted to its own class).
    • Enhanced Encapsulation: Added improved methods to gain access to critical components in the system, promoting safer data access.
    • Method Updates: The singleton() method for creating single user instances has been updated to use these new methods, streamlining operations and minimizing data inconsistencies.
  • Revisions to the FakeStream Class

    • Improved Size Calculation: Adjusted the mechanism for determining the size of an item, which will now use safer getter methods.
    • Method Updates: The isInfinite() method that verifies whether a stream has an end now uses the safer getter method instead of directly accessing components.
    • Enhanced String Representation: Added a new toString() method that will make it easier to output the information of particular objects, improving data representation. The previous version of this method has been discarded.

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.33%. Comparing base (66aeb85) to head (a5c0935).

Files with missing lines Patch % Lines
...main/java/net/datafaker/sequence/FakeSequence.java 85.71% 0 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1779      +/-   ##
============================================
- Coverage     92.37%   92.33%   -0.04%     
- Complexity     3448     3451       +3     
============================================
  Files           339      339              
  Lines          6794     6800       +6     
  Branches        670      670              
============================================
+ Hits           6276     6279       +3     
- Misses          353      354       +1     
- Partials        165      167       +2     

☔ View full report in Codecov by Sentry.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bodiam
Copy link
Copy Markdown
Contributor

bodiam commented Mar 14, 2026

AI PR

@bodiam bodiam closed this Mar 14, 2026
@bodiam
Copy link
Copy Markdown
Contributor

bodiam commented Mar 14, 2026

Don't send us PRs anymore please.

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.

3 participants