Best practice for adding a bidirectional relation in OO model

前端 未结 5 1446
無奈伤痛
無奈伤痛 2021-02-04 09:24

I\'m struggling to come up with a good way of adding a bidirectional relation in OO model. Let\'s say there is a Customer who can place many Orders, that is to say there is a on

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 09:52

    first, unless you plan on moving orders between customers, I think you shouldn't provide a setCustomer() method, the customer should be a parameter for the constructor and leave it unchanged.

    then, the constructor shouldn't be accessible for the user, only use the factory method of Owner.

提交回复
热议问题