MVC "Model - The Model-View-Controller, MVC) structure is designed for applications that need to provide multiple views for the same data, which is good to achieve separation of data layers and representations. For example, the example in the figure below: We see that several sets of data in the figure are expressed in different forms (view), one is a table style, one is a graphic style. MVC divides this application into three object types : Model: Maintaining data and provides data access method. View: Partial data or all data for the model. Controller: Processing events. Here is a typical MVC communication method, the event is processed by the controller, the controller receives User events, and change the model based on the type of event. The view will register in the model in advance. When the model data changes, each view that has been registered to this model is notified. The view takes the latest data from the mold and refreshes yourself. To implement the MVC, the most important part is to use the Observer mode in Design Pattern. Observer mode allows an object to inform multiple observer (Observer) when modifying observed objects. Let's talk about how to use instances Obserer mode implements the MVC program structure. In my example, I want to implement an example of a student age display. Use the list and graphics to display each student's age. When the age changes, the display is automatically updated.
The OBServer interface notifies multiple observes when modifying the observer, usually there is a small interface between the observer and the observer, as follows: / * file: observer.java * / public interface observer {public void DataUpdate (Model Model);} This interface has a DataUpdate (Model Model) method, as long as this interface object is implemented, it has become an observer. Model MODEL is again established a data model.
In my example, I created a data object: / * file: data.java * / public class data {public int value; // Student age value public string name; // Student name} Now create a Model: / * File: model.java * / import java.util. *; Public class model = new arraylist (); arraylist observer = new arraylist (); public model () {super ();} public model (int [] Value, string [] name) {for (int i = 0; i A RegisterObserver (OBServer O) method is also provided to register Observer in Model. View View We want to implement a list of views view1 and a graphic display view view view2 and let them implement the OBServer interface so that when Model data changes, automatically refreshes yourself. / * File: view1.java * / Import Javax .swing *;. import java.awt *;. import javax.swing.border *;. public class View1 extends JPanel implements Observer {Model model; public View1 () {} public View1 (Model model) {try {this.model = model; jbInit ();} catch (Exception e) {e.printStackTrace ();}} private void jbInit () throws Exception {this.setBackground (Color.white); this.setBorder (new TitledBorder (BorderFactory.createLineBorder ( Color.Black, 1), "View1"));} public void PaintComponent (GRAPHICS G) {Super.PaintComponent (G); if (model == null) return; int x = 20, y = 50; int h = g.GetFontMetrics (). getHeight (); for (int i = 0; i }} Private void jbInit () throws Exception {this.setBackground (Color.white); this.setBorder (new TitledBorder (BorderFactory.createLineBorder (Color.black, 1), "View1"));} public void paintComponent (Graphics g ) {Super.PaintComponent (G); if (model == null) return; int x = 20, y = 50; int h = g.GetFontMetrics (). GetHeight (); int width = this.Getwidth (); int Height = this.getHeight (); int sy = height / model.size (); int SX = width / 2; for (int i = 0; i new JScrollPane (); JButton jButton1 = new JButton (); JTextField jTextField1 = new JTextField (); JTextField jTextField2 = new JTextField (); JLabel jLabel1 = new JLabel (); JLabel jLabel2 = new JLabel (); JLabel jLabel3 = new JLabel (); Public controller () {Try {jbinit ();} catch (exception e) {E.PrintStackTrace ();}} private void jbinit () throws exception {data [] data = new data [2]; data [ 0] = new data (); data [1] = new data (); data [0] .name = "ted"; data [0] .Value = 20; data [1] .Name = "joy"; Data [1] .Value = 14; Model.adddata (Data [0]); Model.adddata (data [1]); // Note Two lines below: Register its observer View1 and View2 in the model. Model.RegisterObserver (View1); Model.registerObserver (view2); this.getContentPane (). setLayout (null); jscrollpane1.setbounds (New Rectangle (0, 0, 3, 3)); JButton1.SetBounds (New Rectangle (309, 259, 101, 27 )); JButton1.setText ( "Update"); jButton1.addActionListener (new java.awt.event.ActionListener () {public void actionPerformed (ActionEvent e) {jButton1_actionPerformed (e);}}); jTextField1.setText ( "20 "); Jtextfield1.setbounds (New Rectangle (80, 254, 52, 30)); jtextfield2.settext (" 14 "); jtextfield2.setbounds (New Rectangle (178, 255, 50, 31)); jlabel1.setText "Age:"); Jlabel1.SetBounds (New Rectangle (41, 226, 47, 23)); Jlabel2.Settext ("TED"); Jlabel2.SetBounds (New Rectangle (42, 252, 35, 33));