Presentation is loading. Please wait.

Presentation is loading. Please wait.

MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->

Similar presentations


Presentation on theme: "MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->"— Presentation transcript:

1 MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C --> 15 % Model Users interact with a controller (e.g., buttons), and make changes to the model (e.g., data), which is then reflected in the view (e.g., graph). Control View(s) Draw Graph

2 MVC Relationships The Controller has references to both the view and the model; the controller is the base of the triad. The Model might have a reference to the view, to allow for notification of changes. The model does not know who controls it, however. The View has a reference to the model, for gathering data. View also has reference to the Controller, to notify of successful updates. (References are minimal, perhaps to a base class, to allow swapping of controls.) Model View Controller MVC Relationships

3 import java. awt. ; import java. awt. event. ; import javax. swing
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CounterDisplay extends JFrame implements ActionListener{ JButton incButton, decButton; JTextField counterField; //constructor public CounterDisplay(){ setLayout(new FlowLayout()); incButton = new JButton("INC"); decButton = new JButton("DEC"); counterField = new JTextField("1", 5); //add button and textfield to the frame add(incButton); add(decButton); add(counterField); //register the listener incButton.addActionListener(this); counterField.addActionListener(this); setSize(150,100); setVisible(true); } public void actionPerformed(ActionEvent e){ //if the event is generated by the incButton if (e.getSource() == incButton){ counterField.setText((new Integer(Integer.parseInt(counterField.getText())+1)).toString()); //decButton public static void main(String[] args){ CounterDisplay app = new CounterDisplay(); //create the display instance app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

4 public class Counter{ private int value; //constructor public Counter(int v){ value = v;} //set a value public void setValue(int v) {value = v;} //get current value public int getValue() {return value;} //increment the counter public void inc() {value++;} //decrement the counter public void dec() {value --;} }

5 /. Example of Model-Control-View. class CounterDisplay
/** Example of Model-Control-View * class CounterDisplay * Control and display a Counter object */ public class CounterDisplay2 extends JFrame implements ActionListener{ Counter counter; //the Counter object to be controlled and displayed JButton incButton, decButton; JTextField counterField; //constructor public CounterDisplay2 (Counter aCounter){ counter = aCounter; setLayout(new FlowLayout()); incButton = new JButton("INC"); decButton = new JButton("DEC"); counterField = new JTextField("1", 5); //add button and textfield to the frame add(incButton); add(decButton); add(counterField); //register the listener incButton.addActionListener(this); counterField.addActionListener(this); setSize(150,100); setVisible(true); } public void actionPerformed(ActionEvent e){ //if the event is generated by the incButton if (e.getSource() == incButton){ counter.inc(); System.out.println(counter.getValue()); counterField.setText((new Integer(counter.getValue())).toString()); //if the event is generated by the decButton if (e.getSource() == decButton){ counter.dec(); //if ENTER is press on the text field if(e.getSource() == counterField){ counter.setValue(Integer.parseInt(counterField.getText()));

6 Another Example ShapeEditor


Download ppt "MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->"

Similar presentations


Ads by Google