Java Reference
In-Depth Information
For the commands, we have removed the imports for javax.servlet.* and
javax.servlet.http.* . The servlet will house the controller, and command
objects are used only for the model:
/**
* PostListCommand
* This class returns all posts from a database.
* There are no input parameters. There is one output parameter.
*/
public class PostListCommand {
// Field indexes for command properties
private static final int SUBJECT_COLUMN = 1;
private static final int AUTHOR_COLUMN = 2;
private static final int BOARD_COLUMN = 3;
protected Vector author = new Vector();
protected Vector subject = new Vector();
protected Vector board = new Vector();
// SQL result set
protected ResultSet result;
protected Connection connection = null;
Now, we move on to the instance variables. Commands will frequently have
private instance variables that mirror the state of the underlying model. In this
case, since we copy an entire database result set into vectors instead of using
the result sets directly, we have instance variables that map our model abstrac-
tion nicely. We have some implementation artifacts as well: the column vari-
ables map onto database columns, and we have declared our JDBC result set
and connection here.
Listing 3.5 shows the primary methods in the command architecture: ini-
tialize and execute . In our case, they wrap simple database logic. They can
also be used to provide a simple wrapper around a complex object-oriented
model. With execute and init methods, we can begin to see the benefits of
this design pattern. This entire execute method is dedicated to the execution
of our query. Contrast this with the previous example where we were also
printing the HTML result set. We have begun to break the tight coupling
between user interface and model.
Search WWH ::




Custom Search