Other Design Thoughts

The MatchSet() constructor does the work of calculating the score. If the calculated score isn’t consumed by a client, the effort to compute it is waste. For this reason (among others[33]), avoid doing any real work in constructors.

Change the code to calculate the score when it’s requested:

iloveyouboss/big-5/src/iloveyouboss/MatchSet.java
 
public​ ​class​ MatchSet {
 
// ...
 
 
public​ MatchSet(​Map​<​String​, Answer> answers, Criteria criteria) {
 
this.answers = answers;
 
this.criteria = criteria;
 
}
 
 
public​ ​int​ getScore() {
*
int​ score = 0;
*
for​ (Criterion criterion: criteria)
*
if​ (criterion.matches(answerMatching(criterion)))
*
score += criterion.getWeight().getValue();
*
return​ score;
 
}
 
// ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.