I have a factory that makes objects that depend on an external object, do I pass it in the constructor of the factory?
+1
A:
I'm not sure, but you might want to look at inversion of control and dependency injection. It takes a bit to get your head wrapped around, but it's a pattern for dealing specifically with dependencies.
AaronLS
2010-01-28 07:31:36
+1
A:
Because Factory Method calls object connstructor you should pass all necessary paramenters to factory method. Consider folowing:
class Foo {
}
class Boo {
public Boo(Foo foo) {}
}
static class BooFactory {
public static Boo CreateBoo(Foo foo) {
return new Boo(foo);
}
}
Another alternative, as aaronls suggests, you can use Inversion of Control to reduce such dependencies.
Sergey Teplyakov
2010-01-28 11:16:28
Excellent sir, thank you!
jiji
2010-01-30 02:09:18