I created a pretty simple service that's proven quite useful. The interface
is

*public* *interface* ObjectFactory {


 *public* <T> T build(Class<T> clazz);

}


it's built on top of autobuild and since it's a service you can plug in
different factories. The one I have this


*public* *class* ObjectFactoryImpl<T> *implements* ObjectFactory {

*private* *final* Map<Class, Class> config;

*private* *final* ObjectLocator locator;

 *public* ObjectFactoryImpl(Map<Class,Class> config, ObjectLocator locator)
{

*this*.config = config;

*this*.locator = locator;

}


 @SuppressWarnings({ "unchecked", "hiding" })

@Override

*public* <T> T build(Class<T> clazz) {

*if* ( config.containsKey(clazz) ) {

*return* (T) locator.autobuild(config.get(clazz));

}

*return* locator.autobuild(clazz);

}

}


This allows constructing real objects from interfaces with a config like
this:


@Contribute(ObjectFactory.*class*)

*public* *static* *void* contributeObjectFactory(MappedConfiguration<Class,
Class> configuration) {

configuration.add(PayLaterPayment.*class*, PayLaterPaymentDDB.*class*);

configuration.add(CreditCardPayment.*class*, CreditCardPaymentDDB.*class*);

configuration.add(PayPalPayment.*class*,PayPalPaymentDDB.*class*);

configuration.add(Invoice.*class*,InvoiceDDB.*class*);

configuration.add(InvoiceItem.*class*,LineItem.*class*);

}


What makes this useful is you can build modules that define interfaces and
then do things like


*public* *class* PayPalButton {

 @Parameter

*private* PaymentMethod payment;

 @Inject

*private* ObjectFactory objectFactory;

 *void* onSelectedFromPayPal() { payment = objectFactory
.build(PayPalPayment.*class*); }

}


I also think it would make BeanEditForm even more useful. If there any
interest I'll open a JIRA and add the code.


Thanks

Barry

Reply via email to