Java Reference
In-Depth Information
values we wish to track relating to a DVD). Transfer object is named for one of the more com-
mon uses of this design pattern: creating a single class to transfer data between two separate
systems, usually between a client and a server.
The reason for such an object is fairly simple—it can make your code easier to read, and
improve performance of your system as a whole. Your code will be referencing fields within a
single object, which will make it clearer within your code that the fields are all related. And
since you can send and retrieve the entire value object in one call to another method, your
code will perform much better than if it had to retrieve each of the fields separately. This is
especially useful when retrieving value objects over a network, as only a single network call is
required, in contrast to the multiple network calls required if you were retrieving each field
separately.
Sections of the DVD class are detailed next; however, since much of the code is repetitive,
we won't display all the code here but only important sections of it. The entire code for this
method can be downloaded from the Apress web site in the Source Code section; more details
on what can be downloaded and how the code can be compiled and packaged are listed in
Chapter 9. The line numbers shown here correspond to the real line numbers in the online
source code—therefore, you'll note gaps in the numerical sequence because we skip com-
ments and repetitive code.
1 package sampleproject.db;
2
3 import java.io.*;
4 import java.util.*;
5 import java.util.logging.*;
6
..
13 public class DVD implements Serializable {
..
19 private static final long serialVersionUID = 5165L;
We will be using this class to transfer the data that represents a DVD between the client
and the server, potentially on different computers. Doing this, however, relies on the client
and server both agreeing on what a DVD object is. If either system believes that the internal
data structure is different (for instance, if the server believes that the issue date is a Java Date
object, while the client believes that it is a String object), then the whole system of transfer-
ring data is called into question. If you are lucky, you will receive some form of class cast
exception, or a similar problem. If you are unlucky, your client and server will be able to
work with the data, even though it is potentially wrong, and your data will be corrupted.
Java provides a mechanism for classes that use a serialized object to confirm that the seri-
alized object conforms to a known structure: the serialVersionUID . This is done for us by the
JVM itself; if a class tries to deserialize an instance of a class where the serialVersionUID does
not match the known value, an InvalidClassException will be thrown.
Note Serialization is covered in detail in the first few sections of Chapter 6.
Search WWH ::




Custom Search