From: Eric Schwartz Date: 2003-05-01T07:38:29+09:00 Subject: Re: [OO interface design] Pass-by reference VS encapsulation ? Simon Vandemoortele writes: > 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 ? Hopefully, it wouldn't do that. Or are you assuming you'll never meet two people named Doug Jones? > 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) Why should the Contact care if there's another object with the same data it has? It's the AddressBook's job to tell you things like that. One possible solution is to use an Observer pattern to allow the AddressBook to register itself as being interested in operations like changing names and optionally disallow them. That way you can pass around all the mutable object you like, and let the AddressBook validate any prospective changes. In this particular case, though, I'd invite you to look through a phonebook of even a relatively small city and see how many duplicated names there are. Having some sort of unique key that is not a person's name would be a better solution, IMO. If you'd like to pass around an object and be sure it'll never get altered by its reciever, then a Contact/MutableContact pair seems unavoidable. -=Eric -- Come to think of it, there are already a million monkeys on a million typewriters, and Usenet is NOTHING like Shakespeare. -- Blair Houghton.