Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save trikitrok/cb222d8cb82ee867c6e8f8540264d6d1 to your computer and use it in GitHub Desktop.

Select an option

Save trikitrok/cb222d8cb82ee867c6e8f8540264d6d1 to your computer and use it in GitHub Desktop.

Summary: Make CourseState fields private

What changed

File Change
CourseState.java Fields name, configuration, clock, courseView, durationInMinutes changed from protected to private. Added transitionToOnGoing() and transitionToFinished(Instant) protected methods. Added Instant and ChronoUnit imports.
YetToStartCourseState.java start() now delegates to transitionToOnGoing() instead of directly constructing OnGoingCourseState.
OnGoingCourseState.java end() now delegates to transitionToFinished(startTime) instead of directly computing duration and constructing FinishedCourseState. Removed ChronoUnit import.

Why

Encapsulating CourseState's fields as private enforces the Law of Demeter within the hierarchy: subclasses interact with the parent through its interface (methods), not its data. This makes the base class's internals easier to change without ripple effects across subclasses.

Side effects / follow-up

  • None. The refactoring is purely structural; no behaviour was altered.
  • FinishedCourseState required no changes — it never accessed the fields directly.

Code review checklist

  • All five fields in CourseState are now private.
  • transitionToOnGoing() and transitionToFinished(Instant) are protected (accessible to subclasses, not public API).
  • YetToStartCourseState.start() delegates to transitionToOnGoing().
  • OnGoingCourseState.end() delegates to transitionToFinished(startTime).
  • No subclass directly references a field from CourseState.
  • All existing tests still pass (mvn test).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment