Java constructor with large arguments or Java bean getter/setter approach

后端 未结 18 2192
清歌不尽
清歌不尽 2020-12-12 21:29

I can\'t decide which approach is better for creating objects with a large number of fields (10+) (all mandatory) the constructor approach of the getter/setter. Constructor

18条回答
  •  猫巷女王i
    2020-12-12 22:25

    You might consider using a builder pattern, with the builder ensuring that all the fields are at least set to sensible defaults. Refer to the link for implementation, but you would wind up with a call that looks something like:

    Widget widge = new Widget.Builder(). manufacturer("333").serialNumber("54321").build();
    

提交回复
热议问题