Java is an object-oriented language that implements an object-oriented programming. In actual programming, we should use and play its maximum effectiveness. However, we must use object-oriented programming ideas, and develop a good Java application independently, especially large, medium programs, is not a simple matter. It is based on object-oriented programming idea, people will have a large number of applications, summarizes, summarize, and summarize many standard design patterns. Use these design patterns to use their actual projects to minimize the design problems in the development process to ensure high quality of the project is completed as scheduled.
MVC mode introduction
Model - View-Controller (MVC) mode is designed for applications that need to provide multiple views for the same data. It achieves the separation of the data layer and the representation layer, which is especially suitable for developing applications related to user graphical interfaces, which are shown in Figure 1. The basic structure defined in the mode is:
The controller is used to handle user commands and program events;
Model maintenance data and provides data access methods;
The display of view data.
The basic implementation of the MVC mode is:
1. Controller (such as the main program entry in Java) to create a new model;
2. The controller wants to create a new one or more view objects and associate them with the model;
3. The controller changes the status of the model;
4. When the status of the model changes, the model will automatically refresh the view related to it.
Figure 1 Basic structure of MVC mode
The Java application to be implemented herein is the size and surface area of the sphere when the user enters a sphere in the graphical user interface. We first use the basic MVC mode to implement the above programs, then use different quantities, views, and controller structures to expand the program.
Basic MVC mode
The program is mainly composed of three classes, which are Sphere classes, TextView classes, and spherewindow classes. Where the Sphere class plays the role of Model, the TextView class is a View role, and the SphereWindow class is the Controller role.
Java implements MVC programming mode through a special class OBSERVABLE and OBSERVER interface. Its UML class diagram and the implementation of the MVC mode are shown in Figure 2.
Figure 2 MVC mode UML class diagram
As can be seen from Figure 2, the Model class must inherit the OBSERVABLE class, and the View class must implement the interface OBServer. It is because of the above structure, when the model changes (when the controller changes the model), the model will automatically refresh the view related to it. Its UML sequence diagram can be represented as shown in FIG.
Model class Sphere, must extend the OBServable class, because in the OBSERVABLE class, method addObserver () associates views with the model, and notify the view by method notifyObservers () when the model status changes. The key code for implementing the MVC mode is:
Import java.util.observable;
Class Sphere Extends Observable
{
....
Public Void Setradius (Double R)
{
Myradius = r;
SetChanged (); // Indicates That the model HAS Changed
NotifyObservers ();
}
....
}
Figure 3 MVC mode UML sequence diagram
The role of the VIEW class must implement the interface OBServer, which means that the TextView must be Implements Observe, and the method is also required UPDATE (). With this method, when the status of the model Sphere class changes, the Update () method in the view associated with the model is automatically called, so that the view is automatically refreshed. The key code of the View class is as follows: import java.util.observer;
Import java.util.observable;
Public Class TextView Extends JPanel Implements Observer
{
......
Public void Update (Observable O, Object Arg)
{
Sphere balloon = (sphere) O;
Radiusin.setText ("" f3.format (balloon.getius ()));
VolumeOut.setText ("" f3.format (balloon.volume ()));
SurfareAout.Settext ("" f3.format (balloon.surfacearea ()));
}
......
}
The SphereWindow class is Controller, which mainly creates Model and View, associates View with Model, and handles events, where the key code is:
Public spherewindow ()
{
Super ("Spheres: Volume and Surface Area);
Model = New Sphere (0, 0, 100);
TextView View = New TextView ();
Model.addobserver (view);
View.Update (Model, NULL);
View.addActionListener (this);
Container C = getContentPane ();
C.ADD (View);
}
Public Void ActionPerformed (ActionEvent E)
{
JtextField T = (jtextfield) E.GETSOURCE ();
Double r = double.parsedouble (t.getText ());
Model.setradius (r);
}
This program is written in the MVC mode in Java, with extremely good scalability. It can easily implement the following features:
1. Implement multiple views of a model;
2. Use multiple controllers;
3. When the model changes, all views will be automatically refreshed;
4. All controllers will work independently of each other.
This is the benefit of the Java programming mode, just modify or add new classes on the previous program to easily add many programs. Many of the previously developed many classes can be reused, and the program structure does not need to be changed, and all kinds of independence, facilitate group development, and improve development efficiency.
A model, two views and a controller
Below we discuss how to implement a model, two views, and a controller program. When the user enters the radius of a sphere in the graphical user interface, the program is displayed in the surface area and the surface area, and the sphere is also shown. The schematic diagram of the four classes of the program can be seen in Figure 4.
Figure 4 A model, two views, and a controller's basic structure
The Model class and the View1 class do not need to be changed, as exactly the previous, which is the benefits of object-oriented programming. For the SphereWindows class in Controller, you only need to add another view and you can associate with the Model. Its key implementation code is: public spherewindow ()
{
Super ("Spheres: Volume and Surface Area);
Model = New Sphere (0, 0, 100);
TextView TView = new textView ();
Model.addobServer (TView);
TView.addActionListener (this);
TView.Update (Model, NULL);
GraphicsView gview = new graphicsview ();
Model.addobserver (gview);
gview.update (Model, NULL);
Container C = getContentPane ();
C.SetLayout (New GridLayout);
C.ADD (TVIEW);
C.ADD (gview);
}
The result of its program is shown in Figure 5.
Figure 5 output result
A model, two views and two controllers
In the above program, we can only enter the spherical radius through the keyboard. Now we have modified the above programs, using the mouse to make large, narrow the sphere graphics on the right and can change the radius of the sphere, thereby obtaining the input of the spheroid radius.
At this time, the MCV mode is a model, two views, and two controllers, which can be seen in Figure 6, and the UML class diagram can be represented as shown in FIG.
Where Sphere, TextView is exactly the same as the GraphicsView class. In the main program SphereWindows, this class is not directly used as Controller, which controls the new construction of Controller1 and Controller2. The key code of the program is:
Public spherewindow ()
{
Super ("Spheres: Volume and Surface Area);
Sphere model = new sphere (0, 0, 100);
TextController Tcontroller = New TextController (Model);
GraphicsController gController = new graphicsController (Model);
Container C = getContentPane ();
C.SetLayout (New GridLayout);
C.ADD (TController.getView ());
C.ADD (gcontroller.getView ());
}
Figure 6 A model, two views and two controllers basic structures
Figure 7 A model, two views and two controllers UML class diagrams
When the program SphereWindow runs, move the mouse to the outer circle of the sphere, click drag to realize the amplification and shrinkage of the sphere, and the spherical radius, the surface area and the ball volume are also varied.
summary
As can be seen from the above, the application related to the graphical user interface is implemented by the MVC mode, which is an extremely good scalability, which is the future direction of Java object-oriented programming.