Builder pattern

The builder (English builder ) is a design pattern in the field of software development and belongs to the category of production patterns ( creational patterns ). It separates the construction of complex objects of the representation, thereby the same construction processes can be re-used. The pattern is one of the so-called GoF patterns ( Gang of Four, see Gang of Four ).

Use

Software developers use the builder when

  • Different representations should exist for a complex object,
  • The construction of a complex object is to be independent of the production of components or
  • The design process requires an internal state that is to be hidden from a client.

Typical applications include applications for conversion.

Actors

One can distinguish four actors: Builder KonkreterErbauer, director and product. The constructor specifies an abstract interface for creating parts of a complex object. KonkreterErbauer generates the parts of the complex object by implementing the interface. In addition, he defines and manages the representation generated by it of the product. It also provides an interface for reading out of the product.

The director constructs a complex object using the interface builder. The Director works closely with the builder: He knows which building sequence of the builders tolerate or needed. The director thus decouples the design process from the client. The product represents the complex object to be constructed.

Benefits

The implementations of the construction and the representations are isolated. The builders hide their internal representation before the Director. New representations are easy to insert new concrete builder classes. The design process is controlled at a dedicated location ( in the Director); subsequent changes - such as a multi-phase construction process rather than a single-phase construction - can be implemented without changing the client.

Disadvantages

There is a close coupling between product, concrete builders and the classes involved in the design process.

Variant

You can let themselves implement the interface builder and the product. This saves you may some classes. The product produced " drags " the builder interface his life around with you, so also can be grown from the outside parts of the product later.

Use in the analysis

This pattern is often used in the analysis software for the difficult metaphor.

The variant in which is itself an object method available to " grow " more parts, proven in pipeline -like business processes. The business process as a director instructs the document as a builder to create new parts and mount in itself. For example, can attach notes to a " file processing " a records management in individual steps.

Example

A stock market software keeps share prices in a text file in the following format firmly: Pro Aktiengesellschaft securities identification number, name of the corporation, price and quantity traded are in a line, separated by spaces, stored:

BASF 515100 36.84 2.8504 million 803200 Commerzbank 6.71 17.2313 million ... Now this format is to be converted into a "modern " format such as CSV or XML. If uses a semicolon as a separator in CSV format, so the above file to be converted, for example, in the following:

515100; BASF; 36.84; 2.8504 million 803200; Commerzbank; 6.71; 17.2313 million In XML format, however, the result of the conversion might look like this:

                          515100 < / SIN >                  BASF < / name >                  36.84                  2850400                                    803200 < / SIN >                  Commerzbank < / name >                  6.71                  17231300          The following C program shows the use of the builder pattern in an application for data format conversion, which is easily extendable to other output formats. The Director (Class price data reader ) knows how to read data in Altformat and to parse. He knows a builder who can translate parsed parts in its respective format. All concrete builders, concrete subclasses of the abstract class price data builder. For example, translates the class XMLKursdatenBauer parsed lines in an XML format.

The client may notify the Director of the concrete builder at runtime. Thus, the output format can be changed at runtime.

To support a new output format, only the class price data builder must be implemented accordingly by a concrete subclass, eg by latex price data builder.

It is important to include in this pattern: It's not just the items produced, the complexity possess (which is why the concrete producer care ), but also the product to be whole is a complex object to the production of which takes care of the Director. The Director is therefore the "expert" for the production of the product; he alone knows the necessary individual steps. In the example he only knows how to parse the Altformat and from the new format is to assemble.

# include # include   using std :: string; using std :: cout;   / / Abstract builder   class price data Bauer { public:    virtual void write rate data (const string & wkn, const string & name, const string & price, const string & QUANTITY )                                   = 0;    virtual void initialisiereSchreiben () { }    virtual void exit write () { } };   / / Concrete builder for CSV files   CSVKursdatenBauer class: public course dates Bauer {    / / Here the product, for simplicity, is created on the    / / Written to standard output. ( It could also be in a stream    / / Be written, which is passed in the constructor. )    virtual void write rate data (const string & wkn, const string & name, const string & price, const string & QUANTITY ) {      cout << wkn << ";" The << name << ";" << Rate << ";" ITEM_NOS << << " \ n";    } };   / / Concrete builder for XML files   XMLKursdatenBauer class: public course dates Bauer {    virtual void write rate data (const string & wkn, const string & name, const string & price, const string & QUANTITY ) {      cout << " \ t " << " \ n";      cout << " \ t \ t " << wkn << " < / SIN > \ n";      cout << " \ t \ t " << name << " < / name > \ n";      cout << " \ t \ t " << rate << " \ n";      cout << " \ t \ t " << ITEM_NOS << " \ n";      cout << " \ t \ n";    }      virtual void initialisiereSchreiben () {      cout << " " << " \ n";    }      virtual void finish (write ) {      cout << " " << " \ n";    } };   / / Director   class price data reader { private:    Course dates Bauer * _kursdatenBauer; public:    void setKursdatenBauer (share data Bauer * kb) {      _kursdatenBauer = kb;    }      void parseUndSchreibeKursdaten () {      _kursdatenBauer -> initialisiereSchreiben ();      / / Read line by line from STDIN and decompose into suitable parts      while (! std :: cin.eof ()) {         wkn string, name, price, quantity;         std :: cin >> wkn >> name >> Courses >> ITEM_NOS;         if ( wkn.empty ()) {break; }         _kursdatenBauer -> write rate data ( wkn, name, price, quantity );       }      _kursdatenBauer -> finish write ();    } };   / / Client   int main () {    CSVKursdatenBauer csvKursdatenBauer;    XMLKursdatenBauer xmlKursdatenBauer;    Exchange rate data reader data reader;      kursdatenLeser.setKursdatenBauer ( & xmlKursdatenBauer );    / / Or    / / KursdatenLeser.setKursdatenBauer ( & csvKursdatenBauer );    kursdatenLeser.parseUndSchreibeKursdaten (); } Related Design Patterns

The abstract factory is similar to the builders because they can also create complex objects. But focus is not the structure in the foreground, but the abstraction from the concrete type of the objects produced. The builder often produces a compound ( design patterns ). Or even the builder - - With applications to convert the director is often a Visitor (Visitor) or possibly an interpreter ( design patterns ) of the structure to be converted.

Pictures of Builder pattern

152225
de