As a parser parses a user 's query, the parser sees select terms, class names , and where clause comparisons. After all these pieces are parsed, you can build a Query object that is a translation of the user's input.
The builder pattern applies when "the algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled " [Gamma et al.]. You use a builder to collect the various aspects of a query and then ask the builder for a query after parsing all the input text. Figure 15.13 shows the QueryBuilder class.
Figure 15.13. The QueryBuilder class. A QueryBuilder object collects information about a desired query and then builds the query on demand.
The constructor for QueryBuilder takes a Speller object, described in the next section. The speller lets the QueryBuilder correct the spelling of class names; a QueryBuilder throws a runtime exception if a class name is unknown to the speller.