Constructor with all class properties or default constructor with setters?

前端 未结 9 1954
无人及你
无人及你 2020-12-24 14:03

Following are the two approaches:

  • constructor with all the class properties

Pros: I have to put an exact number of types of parameters so if I m

9条回答
  •  不知归路
    2020-12-24 14:46

    Who says you can't do both? I'd say mandatory properties go into the constructor, optional ones are handled with setters. BTW, who says you always need one setter per property? If two properties belong together conceptually, why not set them together?

    I like the Builder pattern too, but the most important rule is: always use your brain and find the design that best fits the specific problem. There's no one-size-fits-all solution.

提交回复
热议问题