All org.nakedobjects.object.Naked objects have a
title that can be used when presenting the object to the user. These
titles are generated by the title method, which returns
a org.nakedobjects.object.Title object containing a
textual title.
For value objects this is a formatted version of the value, for example
"10:50 am" or "₤3.50". For business objects this is some form
of identifier.
If a business objects has a field that uniquely identifies each
instance, such as an invoice number, then the
title method can simply get the
Title object from that identifying field. The following
method from the City class passes on the title held by
the name value object for that city.
public Title title() {
return name.title();
}
In other cases a business object's title will be derived from multiple attributes.
The Title object provides a set of useful constructors and methods
that assist in creating titles from String objects,
other naked objects (usually those held within an object's own fields),
and other Title objects. For example, for the
Customer class, we may want to create the title from the
customer's first and last names. A Title object can
append various objects to itself and in true overloaded style will
convert numbers, strings and other naked objects to strings before
appending them, and look after all the spacing for you. Consider the
following method:
public Title title() {
Title title = firstName.title();
Title fullTitle = title.append(lastName);
return fullTitle;
}
Assuming that the value held by firstName is
'Robert' and lastName is 'Matthews' then the call
to append will result in the title being 'Robert
Matthews'. If, however, firstName is empty then
the title will correctly be 'Matthews' (with no leading space). Similarly
if lastName is empty then the result will be
'Robert' with no trailing space.
Simple String objects can also be appended for
additional formatting. Changing the above example to:
public Title title() {
Title title = lastName.title();
Title fullTitle = title.append(",", firstName);
return fullTitle;
}
changes the ordering of the names and produces the correct three
variations: 'Matthews, Robert', 'Matthews' and
'Robert'. The joining string is
additional to the delimiting space.
(If you do not require spaces between the elements of a
title, use the concat method instead of
append .)
One other useful thing to remember is that each of these methods
returns the object it was called upon. This allows the
append and concat methods to be
chained, shortening your title method even further.
This is the terse version of the previous code:
public Title title() {
return lastName.title().append(",", lastName);
}
Remember, too, that reference variables for other
business objects aren't guaranteed to refer to anything (they
could be null ) and could also be unresolved (they don't
contain the required information). For this reason it is important not
to use a reference variable directly, and to check that a reference is
not null before using it. This is another reason why you
should use the field's get... method even for internal operations. You can then
use the returned reference (null or otherwise) as a
parameter to one of the Title constructors or methods.
This way the title class will check the reference and if it is null
will deal with it in a suitable fashion without throwing a
NullPointerException .
The Title methods that take the String
default value as their last parameter will display this value when the
reference passed is null . The following example
shows this. This method will work whether or not the
Order field has a value, and
whether or not the Order object's data has been loaded
yet.
public Title title() {
return new Title(getOrder(), "New Order");
}
|