Public fields in a Data Transfer Object

后端 未结 2 1182
夕颜
夕颜 2020-12-28 13:26

In my years of programming I\'ve often made classes that simply group a few variables with their setters and getters. I\'ve seen these types of objects referred to as value

2条回答
  •  旧巷少年郎
    2020-12-28 14:11

    I do not think it terribly bad practice to have a 'settings' or 'theme' or 'style' class with public attributes.

    Modern IDEs with refactoring tools make it trivial to promote an attribute to getter/setter if you want to do any complicated calculations or checks on values at set-time.

    Often in your 'setTheme' or whatever function that consumes these settings classes is a good clean place to do validation.

    When setting settings like this it is usually appropriate to do a deep copied object rather than retaining a reference to a mutable class.

提交回复
热议问题