开发者

Factory pattern with external dependency

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜