Java Reference
In-Depth Information
Table 3.2 The problems that cause the Magic Servlet. The most common causes are poor experi-
ence and porting. Perl is a particularly common port leading to this antipattern, exacerbated by
many poor conversion guides on the Internet and other places showing how to do straight ports
without redesign.
Inexperienced Java programmers will use the design patterns that they
have used the most. Programmers from scripting or procedural back-
grounds are risks.
Perl rewrites
Most Perl programs are not well organized, and porting guides on the Inter-
net do not preach restructuring the applications.
General porting
Porting a bad design to Java will lead to a Java program with a bad design.
Poor tools
Good design is easier with better tools. Some wizards create poorly struc-
tured code.
Solution: Refactor using commands
In the late 1990s, an IBM team led by Dr. Mike Conner did significant and
powerful research on design patterns that could ease the adoption of Internet
standards such as servlets and Java. This team was a key early collaborator for
the creation of JSP and was also instrumental in the production of the early
EJB specifications. To my knowledge, they were among the first to recognize
the power of this family of design patterns. Many of those ideas are captured
simply and effectively in a practical book called Design and Implement Serv-
lets, JSP s and EJB s for WebSphere . Though it is written with WebSphere in
mind, the concepts commute to other platforms as well. Similar models, like
Model 2 in the Jakarta Struts framework, also support this type of model-
view-controller separation.
The hard part of antipatterns is often recognizing them. Once we've iden-
tified an antipattern, we can take steps to refactor it or apply an existing design
pattern. In this section, we will refactor our bulletin board listing servlet. Each
step will get us closer to a Model-View-Controller design pattern. Settling for
any of the intermediate steps of our refactored solution is not wise. The pro-
cess should be followed all of the way to the model-view-controller conclu-
sion. Our solution will deviate slightly, but the finished product will be easily
recognizable as a model-view-controller architecture.
Break out the model
The 10 KB servlet is performing several functions that can be cleanly separated:
Search WWH ::

Custom Search