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

前端 未结 3 535
粉色の甜心
粉色の甜心 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:24

    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.

    ie:

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

    My code has become much simpler to manage without DI.

提交回复
热议问题