Extracting a New Class

The Profile class defines two responsibilities:

  • Track information about a profile.

  • Determine whether and to what extent a set of criteria matches a profile.

We want to split the two responsibilities so that we have two classes, each small and adherent to the SRP. To do so, we plan to extract the code related to the matches responsibility to another class, named MatchSet. As with all refactoring, we seek an incremental path—make a small change, run the tests to make sure they still pass.

The first change: move the calculateScore() logic into MatchSet. Start by changing the code in matches() to declare the intent. Rather than call calculateScore() directly from matches(), construct a new MatchSet object with the information ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.