public final class FactoryModuleBuilder extends Object
public interface PaymentFactory { Payment create(Date startDate, Money amount); }You can name your factory methods whatever you like, such as create, createPayment or newPayment.
constructedType
is a concrete class with an @Inject
-annotated constructor. In addition to injector-supplied parameters, the constructor
should have parameters that match each of the factory method's parameters. Each factory-supplied
parameter requires an @Assisted
annotation. This serves to document that the
parameter is not bound by your application's modules.
public class RealPayment implements Payment { @Inject public RealPayment( CreditService creditService, AuthService authService, @Assisted Date startDate, @Assisted Money amount) { ... } }
AssistedInject
, in order to match the different parameters types of the
factory methods.
public interface PaymentFactory { Payment create(Date startDate, Money amount); Payment createWithoutDate(Money amount); } public class RealPayment implements Payment { @AssistedInject public RealPayment( CreditService creditService, AuthService authService, @Assisted Date startDate, @Assisted Money amount) { ... } @AssistedInject public RealPayment( CreditService creditService, AuthService authService, @Assisted Money amount) { ... } }
module
, install a FactoryModuleBuilder
that creates the
factory:
install(new FactoryModuleBuilder() .implement(Payment.class, RealPayment.class) .build(PaymentFactory.class));As a side-effect of this binding, Guice will inject the factory to initialize it for use. The factory cannot be used until the injector has been initialized.
.implement
.
public interface OrderFactory { Payment create(Date startDate, Money amount); Shipment create(Customer customer, Item item); Receipt create(Payment payment, Shipment shipment); } [...] install(new FactoryModuleBuilder() .implement(Payment.class, RealPayment.class) // excluding .implement for Shipment means the implementation class // will be 'Shipment' itself, which is legal if it's not an interface. .implement(Receipt.class, RealReceipt.class) .build(OrderFactory.class));
public class PaymentAction { @Inject private PaymentFactory paymentFactory; public void doPayment(Money amount) { Payment payment = paymentFactory.create(new Date(), amount); payment.apply(); } }
Assisted
annotation to disambiguate the
parameters. The names must be applied to the factory method's parameters:
public interface PaymentFactory { Payment create( @Assisted("startDate") Date startDate, @Assisted("dueDate") Date dueDate, Money amount); }...and to the concrete type's constructor parameters:
public class RealPayment implements Payment { @Inject public RealPayment( CreditService creditService, AuthService authService, @Assisted("startDate") Date startDate, @Assisted("dueDate") Date dueDate, @Assisted Money amount) { ... } }
public interface FruitFactory { Apple getApple(Color color); } ... protected void configure() { install(new FactoryModuleBuilder().build(FruitFactory.class)); }Note that any type returned by the factory in this manner needs to be an implementation class. To return two different implementations for the same interface from your factory, use binding annotations on your return types:
interface CarFactory { @Named("fast") Car getFastCar(Color color); @Named("clean") Car getCleanCar(Color color); } ... protected void configure() { install(new FactoryModuleBuilder() .implement(Car.class, Names.named("fast"), Porsche.class) .implement(Car.class, Names.named("clean"), Prius.class) .build(CarFactory.class)); }
Provider
as one of its arguments.Constructor and Description |
---|
FactoryModuleBuilder() |
Modifier and Type | Method and Description |
---|---|
<F> Module |
build(Class<F> factoryInterface)
See the factory configuration examples at
FactoryModuleBuilder . |
<F> Module |
build(Key<F> factoryInterface) |
<F> Module |
build(TypeLiteral<F> factoryInterface)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(Class<T> source,
Annotation annotation,
Class<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(Class<T> source,
Annotation annotation,
TypeLiteral<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(Class<T> source,
Class<? extends Annotation> annotationType,
Class<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(Class<T> source,
Class<? extends Annotation> annotationType,
TypeLiteral<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(Class<T> source,
Class<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(Class<T> source,
TypeLiteral<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(Key<T> source,
Class<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(Key<T> source,
TypeLiteral<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(TypeLiteral<T> source,
Annotation annotation,
Class<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(TypeLiteral<T> source,
Annotation annotation,
TypeLiteral<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(TypeLiteral<T> source,
Class<? extends Annotation> annotationType,
Class<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(TypeLiteral<T> source,
Class<? extends Annotation> annotationType,
TypeLiteral<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(TypeLiteral<T> source,
Class<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
<T> FactoryModuleBuilder |
implement(TypeLiteral<T> source,
TypeLiteral<? extends T> target)
See the factory configuration examples at
FactoryModuleBuilder . |
public <T> FactoryModuleBuilder implement(Class<T> source, Class<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(Class<T> source, TypeLiteral<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, Class<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, TypeLiteral<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(Class<T> source, Annotation annotation, Class<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(Class<T> source, Annotation annotation, TypeLiteral<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, Annotation annotation, Class<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, Annotation annotation, TypeLiteral<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(Class<T> source, Class<? extends Annotation> annotationType, Class<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(Class<T> source, Class<? extends Annotation> annotationType, TypeLiteral<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, Class<? extends Annotation> annotationType, Class<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, Class<? extends Annotation> annotationType, TypeLiteral<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(Key<T> source, Class<? extends T> target)
FactoryModuleBuilder
.public <T> FactoryModuleBuilder implement(Key<T> source, TypeLiteral<? extends T> target)
FactoryModuleBuilder
.public <F> Module build(Class<F> factoryInterface)
FactoryModuleBuilder
.public <F> Module build(TypeLiteral<F> factoryInterface)
FactoryModuleBuilder
.