Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Building a Builder


[ 270 ]

else
invoice = new Invoice()
if (value != null && value instanceof Customer)
value.addToInvoices(invoice)
return invoice
}

public void setParent(FactoryBuilderSupport builder,
Object parent, Object invoice) {
if (parent != null && parent instanceof Customer)
parent.addToInvoices(invoice)
}

public void onNodeCompleted(FactoryBuilderSupport builder,
Object parent, Object invoice) {
invoice.save()
}
}

The factory for sales orders is identical to invoices except that we now return true
from isLeaf because a sales order object will always be a leaf node in our tree:


public class SalesOrderFactory extends AbstractFactory {
public boolean isLeaf() {
return true
}

public Object newInstance(FactoryBuilderSupport builder,
Object name, Object value, Map attributes
) throws InstantiationException, IllegalAccessException {
SalesOrder sales_order = null
if (attributes != null)
sales_order = new SalesOrder(attributes)
else
sales_order = new SalesOrder()
if (value != null && value instanceof Invoice)
value.addToOrders(sales_order)
return sales_order
}

public void setParent(FactoryBuilderSupport builder,
Object parent, Object sales_order) {
if (parent != null && parent instanceof Invoice)

http://www.ebook3000.com
Free download pdf