A Simple Example That Makes You Think

Let’s look at an example in Java. Listing 2-1 shows a simple Java class with several clues about its intent in the way it is constructed. Consider the ScoreWatcher class that is part of a system to track sports scores. It encapsulates the function of obtaining the scores from a news feed.

Listing 2-1: A simple Java class to demonstrate intentional construction

class ScoreWatcher {  private final NewsFeed feed;  private int pollFrequency; // Seconds  public ScoreWatcher(NewsFeed feed, int pollFrequency) {    this.feed = feed;    this.pollFrequency = pollFrequency;  }  public void setPollFrequency(int pollFrequency) {    this.pollFrequency = pollFrequency;  }  public int getPollFrequency() ...

Get Quality Code: Software Testing Principles, Practices, and Patterns 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.