D
dgront
I have a factory class, say F, that creates object O.
public class O {
public O(P policy);
}
public class F {
public F(Class policyClass);
public O createNew();
}
To create a new object, an instance of a policy class P must be
created before. Each of objects O has its own policy class instance.
Each time method createNew() in factory F is called, it creates a new
instance of P via reflection.
There are several types of policy rules; thus there are several such
classes. It still works, since all of them have similar constructors.
I need however to store somehow parameters related with a policy that
will be used by factory F. They must be known BEFORE factory will
create any object of type P. I can make a static field in each of my
policy class. How can I read the field having Class object?
I wrote the whole story instead of just asking the question because
one can propose a better solution. The limitations are as follows:
- I want to avoid passing an instance of P to the constuctor of my
factory.
- The parameters that F needs are always the same for each created
object O.
Dominik
public class O {
public O(P policy);
}
public class F {
public F(Class policyClass);
public O createNew();
}
To create a new object, an instance of a policy class P must be
created before. Each of objects O has its own policy class instance.
Each time method createNew() in factory F is called, it creates a new
instance of P via reflection.
There are several types of policy rules; thus there are several such
classes. It still works, since all of them have similar constructors.
I need however to store somehow parameters related with a policy that
will be used by factory F. They must be known BEFORE factory will
create any object of type P. I can make a static field in each of my
policy class. How can I read the field having Class object?
I wrote the whole story instead of just asking the question because
one can propose a better solution. The limitations are as follows:
- I want to avoid passing an instance of P to the constuctor of my
factory.
- The parameters that F needs are always the same for each created
object O.
Dominik