Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory pattern with external dependency

Tags:

c#

factory

I have a factory that makes objects that depend on an external object, do I pass it in the constructor of the factory?

like image 788
jiji Avatar asked Aug 17 '26 23:08

jiji


1 Answers

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.

like image 129
Sergey Teplyakov Avatar answered Aug 19 '26 12:08

Sergey Teplyakov