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

前端 未结 3 536
粉色の甜心
粉色の甜心 2021-02-05 22:22

I\'m developing a simple tree-structured database and I\'m usually setting dependencies or optional settings via a Builder (Builder pattern). Now I\'m not sure when to use for i

3条回答
  •  名媛妹妹
    2021-02-05 23:21

    Builder pattern vs. Dependency Injection

    How are these 2 even close to comparable in your mind?
    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. E.g. 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.

提交回复
热议问题