Pattern:
Builder


Author

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

Intent

"Separate the construction of a complex object from its representation so that the same construction process can create different representations." (Gamma, E., R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Reading, MA: Addison-Wesley, 1995)

Motivation

A reader for the RTF (Rich Text Format) document exchange format should be able to convert RTF to many text formats. The reader might convert RTF documents into plain ASCII text or into a text widget that can be edited interactively. The problem, however 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 (Director in the thumbnail) with a TextConverter object (Builder in the thumbnail) that converts RTF to another textual representation. As the RTFReader parses the RTF document, it uses the TextConverter 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. For example, an ASCIIConverter (ConcreteBuilder in the thumbnail) ignores requests to convert anything except plain text. Each kind of converter class takes the mechanism for creating and assembling a complex object and puts it behind an abstract interface. 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." Applied to this example, the Builder pattern separates the algorithm for interpreting a textual format (that is, the parser for RTF documents) from how a converted format gets created and represented. This lets us reuse the RTFReaders parsing algorithm to create different text representations from RTF documents--just configure the RTFReader with different subclasses of TextConverter

Known Uses

This pattern has been used on the following systems: RTF converter application from ET++ uses a builder to process text stored in the RTF format. (Weinand, A., E. Gamma, and R. Marty. ET++--An object-oriented application framework in C++. In Object-Oriented Programming Systems, Languages, and Applications Conference Proceedings, pages 46-57, San Diego, CA, September 1988. ACM Press.) Builder is a common pattern in Smalltalk-80. (ParcPlace Systems, Mountain View, CA. ObjectWorks\Smalltalk Release 4 Users Guide, 1990.) The Service Configurator framework from the Adaptive Communications Environment uses a builder to construct network service components that are linked into a server at run-time. (Schmidt, D.C. and T. Suda. The Service Configurator Framework: An extensible architecture for dynamically configuring concurrent, multi-service network daemons. In Proceedings of the Second International Workshop on Configurable Distributed Systems, pages 190-201, Pittsburgh, PA, March 1994. IEEE Computer Society.)

See Also

Abstract Factory and Composite patterns

Thumbnail

Keywords

Abstract Factory pattern, Builder pattern, Composite pattern, creational patterns, Gamma patterns, object creation, system parameterization, objects (complex), product build (step-by-step)

Business Domains

any system requiring abstraction for object creation

Problem Forces

need flexibility in creating various complex objectsneed to create complex, aggregate objects

Benefits

allows variance in a products internal representationgives finer control over the construction processisolates code for construction and representation

Implementation Files

Test driver Test.javaProduct class Product.javaPart class Part.javaDirector class Director.javaConcreteBuilder class ConcreteBuilder.javaBuilder class Builder.javaTest driver. testbuilder.cppSimple implementation of the pattern. builder.h



Generated on Fri Oct 20 10:36:42 GMT+02:00 2000 by Framework Studio