Constructor with all class properties or default constructor with setters?

前端 未结 9 1953
无人及你
无人及你 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:37

    You've missed the biggest pro of having a constructor with loads of parameters: it lets you create immutable types.

    The normal way of creating immutable types without huge constructor nastiness is to have a helper type - a builder which maintains the values you'll want in your final object, then builds the immutable object when you're ready.

提交回复
热议问题