From: Brian Candler Date: 2003-05-17T17:57:32+09:00 Subject: Re: [OO design] Objects VS Datastructures On Sat, May 17, 2003 at 05:43:37PM +0900, henq Validation and formatting gives you more than enough reasons to write smart > getters and setters. Some ideas for you: > - If country is USA, than only one of the 52 (?) states are allowed > - If country is USA tha format phone in standard US style > - International address labels are complex. Streetnumber before or after > street, for example. > - If no postal address given, give visit address instead, and the other way > around, (the latter unless p.o. box involved) That imposes orderings in which the attributes can be set (e.g. you must set the country before setting the phone number). It might be a better solution for the Addressbook itself to perform validation at the point where you insert a new or updated entry: that way, when you check out an address and modify it, you can make a series of changes (some of which temporarily leave the entry in an inconsistent state, like country=UK but postcode=US zip code), until you write it back. Alternatively, if you want addresses to be self-validating, it might be better to have a separate class for each country of interest: class Address # general purpose rest-of-world address class USAddress < Address # US zip-code and phone number class UKAddress < Address # UK postcode and phone number ... etc where method 'format_address_label' does the right thing in each case. Then you'd have a factory method for creating new addresses, which would force you to provide the country code first: MakeAddress('US') => returns a USAddress MakeAddress('BE') => returns an Address You still wouldn't be able to modify someone's address from country X to country Y: you'd have to create a brand new Address object to replace the existing Address object. In which case for consistency maybe all Address objects should be immutable? Regards, Brian.