Applied Design Patterns with Java
Creational :: Builder (97) {C ch 7}
Intent
Separate the construction of a complex object from its representation
so that the same construction process can create different representations.
Motivation
A reader for the RTF format should be able to convert RTF
to many text formats, i.e.: plain ASCII text or into a text widget that can be edited. The problem is that the
number of possible conversions is open-ended. So it should be easy to add a new conversion without modifying the reader.
A solution is to configure the RTFReader class with a TextConverter Delegate object that converts RTF to another textual representation. As
the RTFReader parses the document, it uses the TextConverter Delegate to perform the conversion. Whenever the RTFReader recognizes an
RTF token (either plain text or an RTF control word), it issues a request to the TextConverter to convert the token.
TextConverter objects are responsible both for performing the data conversion and for representing the token in
a particular format.
Subclasses of TextConverter specialize in different conversions and formats. The
converter is separate from the reader, which is responsible for parsing an RTF document.
The Builder
pattern captures all these relationships. Each converter class is called a builder in the pattern, and the reader is called the director. The Builder pattern separates the algorithm for interpreting a textual format from how a
converted format gets created and represented. This allows reuse of the parsing algorithm to create different text
representations from RTF documents.