Suppose a class allows some kind of configuration that has some well-known default values but also has the option of defining a custom value. For example, suppose you want to let users configure a person's title. They can choose from a set of standard values (Mr., Mrs., Ms., etc.) but they can also specify a custom (Dr.). What is the most elegant way to put this in an API? Some examples in Java:
public class Person {
public enum Title {MR, MRS, MS, CUSTOM};
setTitle(Title title, String customTitle);
}
This one is quite bad because whenever you specify anything else than Title.CUSTOM the second argument is silently ignored.
So maybe:
public class Person {
public enum Title { MR, MRS, MS, CUSTOM};
setTitle(Title title);
setCustomTitle(String title);
}
I don't really like this one either, because it's not immediately clear that calling one will overwrite the effects of the other method. Is there a more elegant interface that I'm overlooking?