class Address
{
private enum Component
{
NUMBER,
STREET,
STATE,
COUNTRY
}
private Map<Component, String> componentToValue = ...;
}
I’d like my class to contain two methods:
- One to indicate the value of each address component (so I can debug if anything goes wrong).
- One to return the address in a form expected by humans: “1600 Amphitheatre Parkway Mountain View, CA 94043”.
What is the best-practice for Object.toString()? Is it primary meant for #1 or #2? Is there a best-practice for the naming of these methods?
Would you format an address the same way in a SMS message and in an HTML page? Would you format it the same way in English, French and Japanese?
If no, then you have your answer : the presentation does not belong to the object, but to the presentation layer displaying the object. Unless the object is specifically made up for the presentation layer, for example if it is a HtmlI18nedAddress, use toString for debugging.
Consider
DatevsSimpleDateFormat.Datecontains the state andSimpleDateFormatreturns multiple representations.