Web Technologies
|
|
MVC
MVC
1. Introduction:
MVC stands for Model View Controller. This pattern stems from Smalltalk
object-oriented programming. The idea here is to separate the code of
an application from it graphical interface. MVC contains three main
components: the Model, the View, and the Controller.
The model is the code of the application. This code doesn't
contains any specifications how it will be viewed. It contains some
functions used to set its functionality, like setChanged() to modify
the state of the model; and notifyObservers() to notify its viewers
when a change occurs. The related class extends java.util.Observable. It does
some task and broadcasts the info to View and Controller.
The model in MVC can have several views. The View class provides
graphical user interface (GUI) components for a model. It gets
the values declared in the Model class. It is built of AWT or SWING
components. However, views must implement the java.util.Observer interface.
The Controller gets info from View and updates the model whcich in its turn
notify changes to the registred Views.
2. Example:
Here an example that lets moving a luggage over an incline plane.
In the model class, we write the idea; that is: With an array of pictures,
each time a picture is called, the class broadcasts info via the
setChanged(), and notifyObservers() methods:
inclineModel.java
The view class uses Graphics class of the AWT package and updates the
picture:
inclineView.java
The controller instantiates the Mpodel and the View object, sets a frame
and uses Thread class to process the pictures:
inclineController.java
Compile as:
C:\J2EE\MVC>javac inclineModel.java
C:\J2EE\MVC>javac inclineView.java
C:\J2EE\MVC>javac inclineController.java
Run as:
C:\J2EE\MVC>java inclineController
Finally, we get the nine pictures moving.
These moving pictures could be
also made in JavScript. Here is the code:
inclineJavaScriptCode.html
linked to the
ThisScript.js
That gives the following result:
inclineJavaScript.html
© The Scientific Sentence 2008.
|
|
|