Red Hat Web Application Framework 6.1 用户手册

下载
页码 230
114
Chapter 10. Kernel Tutorial
10.2.1.4.
DomainObject
Methods
Several methods are inherited from
DomainObject
that are useful for building other methods for
subclasses. In general, methods are added to subclasses to provide application logic specific to a
business domain. A common case of this are accessors and mutators for data object properties. Such
methods are usually called by process objects or UI components.
10.2.1.4.1. Accessors and Mutators
Because
DomainObject
s wrap data objects, accessors and mutators for the domain object properties
are common methods to provide on a domain object. The
DomainObject
class provides a
set
and
get
that are automatically delegated to the data object member of
DomainObject
. The
set
method
is used to set properties of the domain object to a value. The
get
method is used to retrieve the values
of those properties.
Using the
Note
domain object, the following example demonstrates how to implement an accessor
and mutator for the contained data object’s property.
/**
* Retrieve the title of the note.
*
* @return The title.
*/
public String getTitle() {
return (String) get("title");
}
/**
* Set the title of the note.
*
* @param title A title for the note.
*/
public void setTitle(String title) {
set("title", title);
}
The accessor,
getTitle
works by calling
get
with the name of the property to retrieve. The
get
method returns a
java.lang.Object
, so the return type must be casted to a
String
. The
setTitle
method works by calling the
set
with the name of the property to be set and the value.
The member data object in
DomainObject
is private to prevent access to the data object outside of the
methods, ensuring an interface/implementation barrier. Subclasses of
DomainObject
cannot access
the data object directly but must use methods on
DomainObject
instead. Because this barrier guar-
antees that all access to the properties of the data object are through
set
and
get
methods, subclasses
can add behavior to these methods. For example, the
ObservableDomainObject
adds behavior
to the
set
method that enables observers of the
DomainObject
to be notified when a property is
changed.
10.2.1.4.2. Initialize
The
DomainObject
class provides an initialize method that is executed after the constructor is run.
The Domain Subclasses can override this method and add further initialization logic. However, the
initialize
method on the superclass should be run to ensure that all inherited member variables
are initialized and so that the data object type checking is executed.
A good use of the
initialize
method is to set initial values for data object properties. The
ACSOb-
ject
class uses the
initialize
method to determine the value for the ObjectType property.
/**