FactoryBuilderSupport

SwingBuilder is one of the most used Groovy builders. It follows the standard structure of BuilderSupport but uses the concept of factories to build each node. Seeing that the concept was useful enough for other builders the basic implementation was taken out of SwingBuilder and FactoryBuilderSupport was born (and SwingBuilder was retrofitted of course). How Builders Work

The Factory interface is the basic building block, the builder will call the factory's methods at specific points during node building, let's see them in their invocation order:

But that's not everything FactoryBuilderSupport has to offer. The factories may require contextual information on the current node being built to do its work, onNodeCompleted may require information that it is only available when newInstance is invoked, or newInstance may need to inspect the parent to decide what is the best way to create the node, just to mention a few scenarios, that's why FactoryBuilderSupport enables the following helping methods:

Method

Description

Map getContext()

returns the context of the current node*

Map getParentContext()

returns the context of the parent of the current node**

Factory getCurrentFactory()

returns the factory that built the current node

Factory getParentFactory()

returns the factory of the parent of the current node (if any)

Object getParentNode()

returns the parent of the current node (if any)**

Object getCurrent()

returns the current node*

* Note: In the newInstance(...) method, since the "current" node has not yet been created, getCurrent() and getContext() will return the parent node or parent context of the node that is currently being constructed.

** Note: In the newInstance(...) method, since the "current" node has not yet been created, getParentNode() and getParentContext() will return the grandparent node or grandparent context of the node that is currently being constructed.

The builder is marked as abstract so you are required to create a subclass for your own builders, despite that it doesn't enforce the implementation of any method at all. There are a couple of protected methods though, that when overwritten will give you more control over the builder's internal workings:

TODO document build() methods