2.5.14 Designing Classes With Focused Responsibilities

Video: Benefits of Encapsulation (6:57)
Designing Classes With Focused Responsibilities

The Wildcats window receives user requests. Team manages its roster, Player manages turns, and TrainingArea manages temperature. Give each rule a clear owner.

Place decisions with the state they govern
Responsibilities
Class Owns Useful public behavior
Player Turn count and limit RecordTurn and GetNumberOfTurns
Team Roster membership AddPlayer and GetPlayerCount
TrainingArea Temperature and bounds IncreaseTemperature and GetTemperature
Window User interaction Request operations and display their results

These operations come from the method, readonly, and validation lessons.

Keep one implementation of a rule

The window should not repeat the three-turn test before every Player request. Player already checks it. If the limit changes, its owner is the place to revise and verify the rule.

Likewise, roster storage should remain inside Team. A caller can request a count without knowing how the list is arranged. Objects can collaborate without exposing every field.

Evaluate the interface against real callers

A short access chain can still bypass a rule; a longer chain can still use legitimate public members. Judge ownership, exposed capabilities, and required behavior rather than punctuation alone.

Apply the idea
Check your understanding