Campbell Ritchie wrote:If you go back to an earlier suggestion that you have a prefix (area code, dialling code, etc.) and number stored as separate fields, can you separate a String into its constituent parts in the constructor?
What about a factory method requiring a non‑null locale as a parameter, returning subclasses of PhoneNumber? Then make all your constructors private.
Campbell Ritchie wrote:There are several ways you can store a formatter object.
You can put it into a map as the “V”, but I shall leave it to you to work out what to use as the “K”. You can have subclasses as I suggested here, with formatters being private static fields of each class. If you are using Locales, you might be able to put formatting information into a resource bundle (not sure). You can have it associated with the type enum. But I remain to be convinced that you need a type enum in the first place; do you have an intersection of the range of phone numbers anywhere so there is a landline and a mobile with the same number in the same country? Would you need an additional (?prefix) number to disambiguate them? Ouch! How are you going to describe that in the documentation? Look at that awkward code with two dots in. Why doesn't the toString() method return the phone number correctly formatted? Also a plainToString() method which returns my phone number in the blank formatrather than correctly formatted like this?If you go back to an earlier suggestion that you have a prefix (area code, dialling code, etc.) and number stored as separate fields, can you separate a String into its constituent parts in the constructor?
What about a factory method requiring a non‑null locale as a parameter, returning subclasses of PhoneNumber? Then make all your constructors private.
I you are confining yourself to one country only, then a lot of this current post doesn't apply.