Talk:Builder
>Created the page --Elliot Fisher 23:50, 6 October 2008 (UTC)
--
Thought I might share a link to this "Stateful Builder", where a SAX parser drives a Builder that builds a Composite. The Builder implements the State pattern, and is in a particular state for each XML element that it is currently parsing.
Incidentally, for a lot less code, you can also use use a kind of Interpreter pattern to parse an XML file into a Composite; where the Composite is also an Interpretor. You make yourself a SAXParser, then when you get a "startElement" event from it (for the root XML element) you instantiate the root Composite element, passing the parser to the element's constructor. The root Composite element attaches itself as a listener to the parser. For each "startElement" event the root gets, it creates the appropriate child, to which it passes the parser. The children in turn listen to the parser and make appropriate children for each "startElement" event that they hear. Of course, this lumps the loading function into the Composite, which is a little quick-and-dirty.
You can also pass a DOM (or a JSON object) down into the construction of a Composite in the same way.