Java Reference
In-Depth Information
Here is the code for the
Contact
class:
1
package
stripesbook.model;
public class
Contact {
private
Integer id;
private
String firstName;
private
String lastName;
private
String email;
private
String phoneNumber;
private
Date birthDate;
/
*
Getters and setters...
*
/
@Override
public boolean
equals(Object obj) {
try
{
return
id.equals(((Contact) obj).getId()); }
catch
(Exception exc) {
return false
; }
}
@Override
public int
hashCode() {
return
31 + ((id ==
null
) ? 0 : id.hashCode());
}
@Override
public
String toString() {
return
String.format("%s %s", firstName, lastName);
}
}
The Data Access Layer
Now that we have a model, we need to make it easy for action beans
(the controller) to work with this model. The approach I like to use is
the Data Access Object (DAO). This is basically an object with methods
to create, read, update, and delete model objects. Action beans don't
need to know how objects get stored and retrieved, and the DAO hides
these implementation details.
Since we might want to use different ways of managing model objects,
we'll use an interface to define the DAO methods. Action beans call
methods on this interface and do not need to change if we decide to
swap implementations.
1.
To reduce “noise,” I leave out the
import
statements in code listings from this point on.
Most of the time, I also omit getter and setter methods.
Search WWH ::
Custom Search