This method is in a Scores class, that is set the class variable scores, but the scores is combination of its sub class, for example, scores = sub scores1 + sub scores2, sub1/sub2 is calculated by the subclasses of Scores.
public void setScores (double scores) {
this.scores = scores;
}
public void calculateScore(double sub1, double sub2) {
this.scores = (sub1 + sub2) /2;
}
is there method/design skills/practice to constraint the class can’t alter the scores value (i.e run the setScores), after the calculateScore() just ran.
You could use a flag
calculatedand if it is true, throw an exception insetScores(...). Note that this approach only will enforce that constraint at runtime and you might miss a situation that throws the exception in your test.Generally it would be favorable to enforce the constraint at compile time, but we’d need some more information. Here’s an idea based on guesses on your other constraints:
You could also pass the scores to the constructor if you want
setScores(...)to be called only once. In this case you either completely get rid ofsetScores(...)or make in protected to allow only this class, subclasses and classes in the same package to call it.