Clearing doubts about the builder pattern

后端 未结 8 1116
攒了一身酷
攒了一身酷 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 23:12

    The builder pattern lends itself well to producing immutable classes, but it can still be a good option for mutable classes too.

    A builder is a reasonable design choice in any situation where an object contains many fields that need to be set during construction; particularly if sensible defaults can be chosen for several of the values.

    Whether you use an inner class depends upon your goals. If you wish to force construction through the builder, you can define the builder as an inner class and ensure the outer class only has a private constructor.

提交回复
热议问题