Model-View-Controller |
||
| PGUI: Model-View-Controller | ||
|
Contents: |
MVC ArchitectureModel-view-controller (MVC) -architecture is a combination of design patterns that is especially suitable for GUI programming. A schematic of MVC is is shown in the figure:
As an example of a MVC application a simple Java-application for drawing diagrams is presented. The application will be further developed in the following material. See the complete example. ModelThe model is the information that the application is manipulating. This is the data representation on the real-world objects in which the user is interested. In Java we can use the class In our example application the model consists of nodes and connecting
lines or connectors. The model is in class The model notifies each view of the changes in the model data. Every
time a node is created, moved or otherwise changed the diagram model calls two
functions of the superclass ( public class Diagram extends Observable ViewThe view implements the visual display of the model. There can be several different views of the same model. Anytime the model is changed, each view of the model is notified so that it can change the visual representation of the model on the screen. A region of the screen that is no longer consistent with the model information is called damaged (or invalid). When notified of a change, the view will identify the changed parts of the display and report them to the windowing system as being damaged. The windowing system will then cause the damaged region to be redrawn (or updated). The view will do the redrawing. In Java we can use interface In our example, the view is implemented in class
The implementation of the view has been simplified so that a change in the model causes the entire view to be redrawn. This is accomplished in function public class DiagramView extends JPanel implements Observer
The view decides on the actual visual representation on the diagram. The only thing in addition to the connections are the titles and the center point of the nodes. Thus the views needs to maintain some information of the bounding rectangles of the nodes (a map data structure Notice that the view always waits for a notification about a change coming from the model before acting upon it. ControllerThe controller receives all the input events from the user and translates them into possible changes on the model. In the example the controller receives the mouse and mouse motion events from the view, processes the interactions and changes the model. The canges will then be visible after the normal notification process has been made (model notifies the view). The controller communicates with the view to determine whet object is being manipulated. The separation of the controller and the view is based on the possibility of implementing several different controllers for one single view. This is not very common and the controller and the view are so tightly intertwined, that they are usually implemented in one class. In out example the controller is an inner class of the view. It can be found
in the class Putting MVC togetherThere are many ways to connect the model, the views and the controllers together in the application. In our example we
A more finished application structure would connect creating MVC to the opening and creating of the documents. Our demo application has one final class Why separate model and view?In a simple application it is empting to combine model, view and the controller into a single class or global variables. The are reasons not doing this:
|
See also: |
| Jarkko Leponiemi, Thursday, 16-Oct-2003 11:50:35 EEST | ||