8 KB Servlet
Breaking out the command is the first step in refactoring the Magic
Servlet. The Command is a design pattern that allows clean wrapping of
the business logic, or model. Later steps will break out the return-trip
It is serving as the model for the program. The business logic is entirely
or partially included in the servlet.
It is serving as the controller for the program. It is handling the input
It is performing the validation of the input data.
As we mentioned earlier, this design makes it difficult to make changes in busi-
ness process without corresponding changes in the code that returns the
HTML to the server, the code that reads from the business model, the error-
handling code, and the code that marshals data between the user interface and
the business logic. Changes in the returning HTML are code changes instead
of simple editor changes. Validation and marshaling changes are similarly
Figure 3.5 shows how we should begin to refactor this solution. We should
break out the model for the program into a distinct component. Fortunately,
several design patterns exist that do just that. We could create an object model
to handle the back-end logic. We could package the components of this model
as EJB s, a solution explored in chapter 8. Or, we could explore a command
Wrapping the model with command objects
Several design patterns place a thin wrapper of code around the business logic,
or model, for convenience. The Facade design pattern in chapter 8 is essen-
tially a thin wrapper with a different interface. The Jakarta Struts framework
uses a similar wrapping concept, called actions. A third alternative is the Com-
mand design pattern, which has been around since the early days of Smalltalk.
Internet search engines provide a wealth of information about commands.