Is it just me or what you're proposing is very similar to what StrategyBuilder.build(Class, Map) already does in the sense that you want to associate certain objets to a given class (the config field in ObjectFactoryImpl)?

On Fri, 27 Sep 2013 15:32:00 -0300, Barry Books <[email protected]> wrote:

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


--
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to