tags:

views:

72

answers:

2

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
+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
Excellent sir, thank you!
jiji