Hi Mike,
Thanks for the posting [1], which I've just answered. For the benefit of isis-dev, I've inlined the answer below your original question also.

On 11/01/2011 12:02, Mike Burton wrote:
Hi Dan,

...

I posted a question on the PragProg discussion section, guess it will forward 
to you soon but emailing just in case...

re Chapter 3 : 3.1 Associating Objects : Scalar (remove) – Page 69 of PDF 
version P1.0

In the Customer.removeFromCars() method, why do you call:
        car.setOwningCustomer(null);
and not:
        car.clearOwningCustomer();?



The short answer is that calling clearOwningCustomer() would generate an infinite loop.

The longer answer is… those pages (try to) explain the mutual registration pattern. The idea is that we have a bidirectional relationship (1:m between Customer and Car in this case) and want to maintain referential integrity so that if a Car is in a Customer’s collection, then that Car will point back to its owning Customer.

How this is done is that we choose one side (I chose Customer in this case) to be “in control” of the relationship, while the other side (Car) just delegates up to Customer.

Hence, in Customer’s addToCars/removeFromCars methods it populates its own collection and directly sets the Car’s reference. In the Car’s modifyOwningCustomer/clearOwningCustomer methods is simply delegates up to the Customer’s addTo/removeFrom methods, and doesn’t do any other work itself.

Thus, if Customer’s addTo/removeFrom incorrectly called modify/clear, which then called back to addTo/removeFrom, then we’d get an infinite loop.

Hope that makes sense!


~~~~~
Cheers
Dan

[1] http://forums.pragprog.com/forums/106/topics/8455

Reply via email to