Chapter 10
[ 271 ]
parent.addToOrders(sales_order)
}
public void onNodeCompleted(FactoryBuilderSupport builder,
Object parent, Object sales_order) {
sales_order.save()
}
}
All of the intelligence of how to orchestrate the construction process is encapsulated
in the FactoryBuilderSupport class. So, literally all we need to do for the whole
builder to work is to register the Factory classes with appropriate tag names:
public class CustomersFactoryBuilder extends FactoryBuilderSupport {
public CustomersFactoryBuilder(boolean init = true) {
super(init)
}
def registerObjectFactories() {
registerFactory("customers", new CustomersFactory())
registerFactory("customer", new CustomerFactory())
registerFactory("invoice", new InvoiceFactory())
registerFactory("sales_order", new SalesOrderFactory())
}
}
FactoryBuilderSupport uses reflection at runtime to detect what registration
methods to run. By scanning the list of methods in the MetaClass instance
and looking for methods that begin with register, FactoryBuilderSupport
detects whether any additional registration methods are provided in the derived
builder class. In the preceding example, the only registration method added is
registerObjectFactories, but we could well have written code:
def registerCustomers() {
registerFactory("customers", new CustomersFactory())
}
def registerCustomer() {
registerFactory("customer", new CustomerFactory())
}
def regiaterInvoice() {
registerFactory("invoice", new InvoiceFactory())