4

I am reading GoF, and the intent of builder is mentioned as to separate creation of complex object from its representation.

I couldn't understand what representation means in this context. What does it mean?

The Text parsing example provided in the motivation section doesn't seem to separate construction from representation, but algorithm for interpretation of textual format from creation and representation of converted format.

enter image description here

4

The representation of interest is here the (presumably) string result obtained from GetASCIIText or GetTexText.

Someone has to create an instance of the abstract TextConverter; this is not shown within the diagram you posted in the question.

Some code somewhere chooses between one of the three sub classes to instantiate and then supplies that to the RTFReader.

This might look as follows:

var tx = new TexConverter ();
var rdr = new RTFReader ( tx );
rdr.ParseRTF ();
return tx.GetTexText ();

and, in another case,

var cv = new ASCIIConverter ();
var rdr = new RTFReader ( cv );
rdr.ParseRTF ();
return cv.GetASCIIText ();

With this additional context, we can see more clearly the separation of construction responsibilities from representation responsibilities, as ParseRTF is reused unchanged between these two snippets even though each snippet generates a different representation.

Further, if we wanted to, we could create an implementation for TextConverter that wrapped the other three and created all those representations with one call to ParseRTF. This would not be as easy to do if these responsibilities were not separated.

It should be pointed out that in this example, the resulting data structures that are constructed do not necessarily have to conform to each other, so, GetTexText, could, if desired, return a type different from string and different from GetASCIIText; this would illustrate vastly different representations.

6
  • RE : "separation of construction responsibilities " , here in the given example ParseRTF, isn't constructing anything(not even parse tree), only interpreting. Please correct if I am wrong. – q126y Jan 1 '16 at 18:13
  • 1
    It has the responsibility for the algorithm that directs the construction. We can consider that it is constructing in the abstract. – Erik Eidt Jan 1 '16 at 18:15
  • I think, I would have not asked this question if names of function were like like addCharacter addFont, instead of convertXXX in ParseRTF, it would have been more intuitive then. – q126y Jan 1 '16 at 18:36
  • @ErikEidt could you more succinctly describe what an object's representation is? – Willwsharp May 24 '19 at 15:03
  • The complex objects they're talking about are the outputs that need to be created by the common algorithm. The inputs here are RTF files, and the outputs vary in type. For GetASCIIText could be a simple string; for GetTeXText it could be a string serialization of TeX format, or a collection of TeX objects or a pointer to a root TeX object; for GetTextWidget it could be a set of graphical user interface objects. So the complex objects are high-level domain concepts possibly compositions of multiple object in the low-level programming environment. – Erik Eidt May 24 '19 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.