Java Reference
In-Depth Information
The Model-View-Controller Pattern
The technique we advocated in the previous Programming Tip is an example of a general
technique known as the Model-View-Controller pattern. Display 17.13 gives a dia-
gram of this pattern. The Model part of the pattern performs the heart of the applica-
tion. The View part is the output part; it displays a picture of the Model's state. The
Controller is the input part; it relays commands from the user to the Model. Each of
the three interacting parts is realized as an object with responsibility for its own tasks.
In a simple task such as the JFrame in Display 17.11, you can have a single object with
different methods to realize each of the roles Model, View, and Controller.
To simplify the discussion, we have presented the Model-View-Controller pattern
as if the user interacts directly with the Controller. The Controller need not be under
the direct control of the user, but could be controlled by some other software or hard-
ware component. In a Swing GUI, the View and Controller parts might be separate
classes or separate methods combined into one larger class that displays a single win-
dow for all user interactions.
Model-View-
Controller
Self-Test Exercises
23. Suppose you omitted the method actionPerformed from the class in Display
17.11 and made no other changes. Would the class compile? If it compiles, will
it run with no error messages?
24. Suppose you omitted the method actionPerformed and the phrase implements
ActionListener from the class in Display 17.11 and made no other changes.
Would the class compile? If it compiles, will it run with no error messages?
Display 17.13 The Model-View-Controller Pattern
Model
data1
data2
.
.
.
Manipulate
Notify
View
Controller
...
...
update()
...
...
 
Search WWH ::




Custom Search