Java Reference
In-Depth Information
If the back-end logic is not well defined, then the application can easily
adopt the user interface as the design. The programming scripts, in the form
of basic functions, are attached to user interface events at key places. The big-
gest event, like a Submit or OK pushbutton, usually attracts the most code,
much like the jam side of a piece of toast attracts the floor. The Submit func-
tion becomes the focal point of the application and, without the appropriate
organization, grows like an unmanageable blob.
Building with Model-View-Controller
Of course, the right way to design this type of application is by using one of
the most famous and earliest design patterns: Model-View-Controller. This
common design pattern has become a staple of modern programming. It is
mentioned in the introductory chapters describing the utility of design pat-
terns in the design pattern bible, Design Patterns , by the Gang of Four. It has
been refined and updated in such places as Core J2EE Patterns , by Deepak
Alur, et al., and the Jakarta Struts framework. It has served as the foundation
for frameworks in Java, C, C++, Smalltalk, LISP , and many others. In this
chapter, we will introduce a variation called the Triangle pattern . With this
pattern, the model is the business logic that drives the application. The user
interfaces, called views , then present various aspects of the model. The interac-
tion of the user and other input / output streams are managed with the control-
ler. This design pattern is shown in figure 3.2.
Failing to separate model and view
Listing 3.1 is a program, written by a novice, that fails to use Model-View-
Controller. This baby is admittedly too ugly for even a mother to love, but
Java like this is more common than any of us would like to admit, especially in
Figure 3.2 Model-View-Controller is a design pattern that advocates clear separation of the
user interface, or view , and the business logic, or model . The controller helps to manage input
and output. The advantages are widely recognized as easier maintenance, improved flexibility,
and more readable code.
Search WWH ::

Custom Search