5

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?

2 Answers 2

5

You could overload the setTitle mthod

public class Person{
    public enum Title { MR, MRS, MS, CUSTOM };
    private Title EnumTitle
    private String CustomTitle;

    public void setTitle(Title title); //Does what you'd expect
    public void setTitle(String title)
    {
        EnumTitle = Title.CUSTOM;
        CustomTitle = title;
    }

    /**
     * This does present some problems when returning the value, however.
     *
     * My first thought to solve this problem is to always expect the 
     * output of getTitle() to be a string - either the custom title or
     * a string representation of the enum value.
     */ 
}
1
  • Thanks. This approach solves the problem where it's unclear that the different methods overwrite each other's effects (since it's always clear that subsequent calls to the same set method will overwrite previous invocations). One problem is however what you mention: if you require internal representation as a string you lose some knowledge of whether it was custom or not (or you have to maintain the value of two variables, which may not be so bad...)
    – Deckard
    Feb 6, 2012 at 11:49
1

Is there a reason why you need the title enumeration or is this just an implementation detail?

Depending what you want to do with the api this could be a simple solution.

public class Person {
  public static final String MR = "MR";
  public static final String MRS = "MRS";
  public static final String MS= "MS";

  public void setCustomTitle(String title);
}

Maybe it is a good idea to distinguish title from sex if this is necessary for your processing

  public enum Sex { Female, Male, };
  public void setSex(Sex sex); //Does what you'd expect
1
  • Thanks. Well, I would like to keep track of whether the title ends up being a standard or a custom value. I suppose I could do that here, but I'd end up having to compare strings to figure that out afterwards.
    – Deckard
    Feb 6, 2012 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.