Player can expose RecordTurn while keeping the small operation that updates its count private. Callers need the action, not every implementation step.
public class Player
{
private int numberOfTurns;
public void RecordTurn()
{
this.IncreaseTurnCount();
}
private void IncreaseTurnCount()
{
this.numberOfTurns = this.numberOfTurns + 1;
}
public int GetNumberOfTurns()
{
return this.numberOfTurns;
}
}RecordTurn calls the helper on the same Player. The helper reads the existing count, adds 1, and stores it. When it finishes, control returns to RecordTurn and then its caller. This small version has no turn-limit check yet.
Run these statements inside an outside method.
Player jordan = new Player();
jordan.RecordTurn();
jordan.RecordTurn();
int turns = jordan.GetNumberOfTurns();| Moment | Count |
|---|---|
| After creation | 0 |
| After first RecordTurn | 1 |
| After second RecordTurn | 2 |