Simon Vandemoortele wrote:
>>So, I'd have a method in Contact that changes an immutable Contact into 
>>a mutable one. 
> 
> 
> By 'changing into' you mean create a new instance right ? (Methinks we
> would be in trouble if an immutable contact could magically become
> mutable in the hands of a client.)

It's be a new instance of a new class.

>>For simplicity I'd also have AddressBook set itself up as the backing
>>store for Contacts it creates.  That way you just have to write
>>'contact.save' rather than carry both the Contact and AddressBook
>>objects around throughout your code.
> 
> 
> That looks very practical but it does induce strong coupling of the
> Addressbook and Contact class.

Not particularly: the Contact object you return could actually be a 
StorableObject that wraps the actualy contact object and adds the #save 
method: Contact need know nothing about the AddressBook.

> Once we go in that direction I don't see
> why I shouldn't stick with my original (fully mutable) implementation
> where contacts callback on the addressbook when they get a request to
> change.

Validation might be an issue: see below.

> Besides: what would happen if you call #save on a contact and the
> addressbook refuses the save because there's already an entry with that
> name ? Wouldn't it be better if the Contact consulted the addressbook as
> soon as the duplicate name was entered ? (and not after all sorts of
> other attributes have been set on the contact)

It depends on your application's semantics. Most of the time, objects 
representing persistent state are allowed to be inconsistent for a short 
time with repect to the database: they only come in to line when you try 
to persist them. This means that cross-field validation is often 
performed at a commit point (such as the save). Otherwise you run in to 
problems when two fields interact in the validation process.


Think of it the other way around: if you're worried about folks updating 
contacts that they receive when they don't really mean to persist those 
changes, would you want the default behavior to be to store all changes 
that are made?  :)



Cheers


Dave