Factory template with external dependency

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

+3
source share
2 answers

Since the Factory method calls the connstructor call, you must pass all the necessary parasites to the Factory method. Consider the following:

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.

+2
source

I'm not sure, but you can take a look at control inversion and dependency injection. It takes a little to wrap your head, but this is a template for working with dependencies.

+1
source

Source: https://habr.com/ru/post/1730336/


All Articles