I have a factory that makes objects that depend on an external object, do I pass it in the constructor of the factory?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With