Perhaps one could go with the approach scala uses for its functions.
i.e.
interface Function1<R,P0> {
R apply(P0);
}
interface Function2<R,P0,P1> {
R apply(P0,P1);
}
so you have the generic type parameters for type safety and the apply i.e.
create method with the def as default return.
applying the parameters to the object under construction could be either a
simple constructor call which takes exactly
these parameters in this order. (constructor.newInstance(args))
interface ValueFactory1<R,P0> {
R create(P0);
}
...
so the concrete factory would be an
new ValueFactory2<Def,String,Integer>() {
public Def create(String habba,Integer zout) {
return new Def(habba,zout);
}
}
or a default constructor and setter methods for the parameters (there you have
the difficulty of retrieving the correct
names and/or the order).
one could also use a generic value object that implements the given interface
(Def) and just keeps the values in a Map
and returns the properties as needed so the setup could be as easy as passing
the map to the generic value object (like
the literal map constructors of groovy).
so, taking a proper value factory, creating a dynamic proxy for it and applying
the invoke to the map-based constructor
of the generic value object may be a solution.
Michael
Niclas Hedhman schrieb:
> Yes, interesting question this one, since you on one hand want to keep
> code free of Qi4j specific stuff as well, right.
>
> So, let's say we have
>
> public interface Def
> {
> Property<String> habba();
> Property<Integer> zout();
> }
>
> public interface Abc
> {}
>
> where Abc needs to create Def instances for some reason. Both are
> composite in nature, but we want to avoid @Structure injection in
> either one.
>
> so in the Abc mixins could do;
>
> public class AbcMixin
> implements Abc
> {
> private DefFactory factory;
>
> public AbcMixin( @Service DefFactory factory )
> {
> this.factory = factory;
> }
> :
> :
> Def def = factory.create( args );
> :
> :
> }
>
> That is fairly straight forward.
>
> Can this be made more generic??
>
> Well, the minimalistic view would be;
>
> public interface InstanceFactory<T>
> {
> T create( Object... args );
> }
>
> But that makes the argument-to-prototype state setting impossible...
>
> How about?
>
> public interface InstanceFactory<T>
> {
> }
>
> public interface DefFactory extends InstanceFactory<Def>
> {
> Def create( String habba, Integer zout );
> }
>
> but AFAIU, it is not possible to extract the names of the arguments
> and type alone is not good enough, so no matching can be inferred by a
> generic implementation.
>
> Then we end up with
>
> public interface DefFactory extends InstanceFactory<Def>
> {
> Def create( Def template );
> }
>
> where the Def provided is "cloned" from a POJO or a Composite.
>
> But then you still end with a lot of lines of code to set up the Pojo.
>
> So, all in all, I think that custom made factories seems to be the
> simplest way forward. But I wanted to elaborate on my thinking, and
> asking if anyone else see some other way that is better....
>
>
> Cheers
> Niclas
>
> _______________________________________________
> qi4j-dev mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/qi4j-dev
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev