简体   繁体   English

构建器模式与依赖注入(例如通过Guice)

[英]Builder pattern vs. Dependency Injection (for instance via Guice)

I'm developing a simple tree-structured database and I'm usually setting dependencies or optional settings via a Builder (Builder pattern). 我正在开发一个简单的树形结构数据库,我通常通过Builder(Builder模式)设置依赖项或可选设置。 Now I'm not sure when to use for instance Guice, when to use the Builder pattern and when to use a static factory method instead of the constructor itself. 现在我不确定何时使用Guice,何时使用Builder模式以及何时使用静态工厂方法而不是构造函数本身。 I've read Effective Java several times and I think it mentions at least a lot of advantages for not exposing the constructor. 我已经多次阅读过Effective Java,我认为它至少提到了不暴露构造函数的许多优点。 It's time to reread ;-) 是时候重读了;-)

So, do you know of cases which are clearly distinguishable? 那么,您是否知道明显可区分的案例? And shouldn't I expose the constructor? 我不应该暴露构造函数? Thus for instance in every case write public static Foo getInstance(...) { return new Foo(...)} ? 因此,例如在每种情况下写public static Foo getInstance(...) { return new Foo(...)}

I'm a firm believer in that you don't need to use dependency injection for everything . 我坚信你不需要为一切使用依赖注入。

  • For a LookupService it would be natural inject a Dictionary such that its implementation can be swapped out by configuration. 对于LookupService ,自然会注入一个Dictionary ,使其实现可以通过配置进行交换。

  • For a Firewall on the other hand. 另一方面,对于Firewall It would be natural for it to create its own FireWallRules , perhaps through a supplied Factory or a Builder . 它很自然地可以通过提供的FactoryBuilder创建自己的FireWallRules

As a guideline, inject what you need to configure , don't automatically inject everything else. 作为指导,注入您需要配置的内容 ,不要自动注入其他所有内容。


Consider a static factory (*) when 考虑一个static factory (*)

  • named construction logic is desired. 需要命名构造逻辑。 Eg, Lists.newArrayList() 例如, Lists.newArrayList()
  • the construction is so complicated it doesn't belong in the class itself 建筑如此复杂,不属于班级本身
  • no configuration of the factory is required, and the factory has no side effects 不需要工厂配置,工厂没有副作用

Consider instance factories when 考虑instance factories

  • there is complex instantiation logic 有复杂的实例化逻辑
  • configuration of the factory is needed 需要配置工厂
  • using AbstractFactory design pattern 使用AbstractFactory设计模式
  • there's need to create additional objects throughout the programs lifecycle 需要在整个程序生命周期中创建其他对象

Consider a builder when 考虑一个builder

  • there are complex parameter choices. 有复杂的参数选择。 Eg, 5 parameters where some are optional. 例如,5个参数,其中一些是可选的。

(*) Static methods are not always testable and the presence of one should in my opinion always be motivated . (*) 静态方法并不总是可测试的,在我看来,一个人的存在总是有动力的 A typical usecase for a factory is to decrease coupling . 工厂的典型用例是减少耦合 By using a static factory that ability is completely lost. 通过使用static factory ,能力完全丧失。

Builder pattern vs. Dependency Injection 构建器模式与依赖注入

How are these 2 even close to comparable in your mind? 这些2在您的脑海中甚至接近可比性如何?
The builder pattern is used when you need to deal with classes whose constructors would have an overwhelming number of parameters (potentially optional) and this pattern makes your code easier to read and write. 当您需要处理构造函数具有大量参数(可能是可选的)的类时,将使用构建器模式 ,此模式使您的代码更易于读写。

Dependency Injection is an approach that facilitates loose coupling removing the dependencies of higher level classes to lower level classes. 依赖注入是一种有助于松散耦合的方法,可以将更高级别类的依赖性移除到更低级别的类。 Eg a class that needs to connect to a database does not directly create a connection but a connection is "injected" and this connection could be swapped to a different database without affecting the code using it. 例如,需要连接到数据库的类不会直接创建连接,而是“注入”连接,并且可以将此连接交换到其他数据库,而不会影响使用它的代码。

I have started using builder for most of my projects and it turns out I can and have replaced all of my DI with builders and singleton. 我已经开始在我的大部分项目中使用构建器,事实证明我可以用构建器和单例替换所有的DI。

ie: 即:

AppContext appContext = new AppContext.Builder()
.setProperties(testProps)
.setDB(testDB)
.build();

// run tests

My code has become much simpler to manage without DI. 没有DI,我的代码变得更加简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM