Clearing doubts about the builder pattern

后端 未结 8 1134
攒了一身酷
攒了一身酷 2020-12-28 22:57

I am learning about the builder pattern, and so far I understood that, it is a great alternative to the commonly patterns used for initialization:

  • Telescopi

8条回答
  •  Happy的楠姐
    2020-12-28 23:03

    Is it possible to implement the builder pattern without using static inner classes?

    Absolutely, yes. As far as the Builder design pattern is concerned, it does not make any difference if the Builder is an inner class or not.

    -If I have to use the builder pattern by using the inner class, do you think it is correct to keep the getters and the setters?

    Yes, it is ok. it is kind of like, build the object using a certain template and then customize it, if needed.

    -I did a little example for practice where I tried to avoid the inner class. Could you let me what do you think about it?

    Two problems -

    1. the example does not justify the usage of Builder pattern. Builder pattern is used to build a complex object. So if you can simply build product as : Product p = new Product(); p.setColor(c); p.setPrice(prc); Then there is hardly any benefit in the way you have shown.
    2. Product should not have a dependency on Builder.

提交回复
热议问题