Considering object encapsulation, should getters return an immutable property?

前端 未结 11 565
余生分开走
余生分开走 2020-12-20 11:38

When a getter returns a property, such as returning a List of other related objects, should that list and it\'s objects be immutable to prevent code outside of

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 12:19

    I think you'll find that it's very rare for every gettable to be immutable.

    What you could do is to fire events when a property is changed within such objects. Not a perfect solution either.

    Documentation is probably the most pragmatic solution ;)

提交回复
热议问题