Model-View-Controller (MVC) Structure
Previous - Presentation-Model

Here the same finance calculator is structured according the the Model-View-Controller (MVC)pattern. The concept is to individual the user interface (the Demonstration in the past example) into a Perspective (creates the show, contacting the Style as necessary to get information), and Operator (responds to customer demands, getting both the Perspective and Operator as necessary). The literary works on MVC results in space for a variety of modifications, but they all adhere to this essence. This model is easy and can be used with easy technique contacting. If there are more complicated communications (eg, the Style is asynchronously updated), an Viewer design (listeners) may be needed.
An outstanding details of MVC is Sun's content Coffee SE Program Style With MVC. Another details of MVC can be discovered at the starting of the guide Implementing the Model/View/Controller Style Model in VisualAge for Coffee.
Main program
The primary system initializes everything and connections everything together.
An outstanding details of MVC is Sun's content Coffee SE Program Style With MVC. Another details of MVC can be discovered at the starting of the guide Implementing the Model/View/Controller Style Model in VisualAge for Coffee.
Main program
The primary system initializes everything and connections everything together.
| // structure/calc-mvc/CalcMVC.java -- Calculator in MVC design.
// Sam Swartz -- Dec 2004
import javax.swing.*;
public category CalcMVC {
//... Make model, view, and controller. They are
// designed once here and approved to the areas that
// need them so there is only one duplicate of each.
community fixed gap main(String[] args) {
CalcModel model = new CalcModel();
CalcView view = new CalcView(model);
CalcController controller = new CalcController(model, view);
view.setVisible(true);
}
}
|
View
This Perspective doesn't know about the Operator, except that it provides techniques for applying a Controller's audience. Other companies are possible (eg, the Controller's audience are non-private factors that can be recommended by the Perspective, the Perspective contacting the Operator to get audience, the Perspective contacting techniques in the Operator to procedure activities, ...).
| // structure/calc-mvc/CalcView.java - Perspective component
// Demonstration only. No customer activities.
// Sam Swartz -- Dec 2004
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class CalcView expands JFrame {
//... Constants
personal fixed last Sequence INITIAL_VALUE = "1";
//... Components
personal JTextField m_userInputTf = new JTextField(5);
personal JTextField m_totalTf = new JTextField(20);
personal JButton m_multiplyBtn = new JButton("Multiply");
personal JButton m_clearBtn = new JButton("Clear");
personal CalcModel m_model;
//======================================================= constructor
/** Constructor */
CalcView(CalcModel model) {
//... Set up the logic
m_model = model;
m_model.setValue(INITIAL_VALUE);
//... Initialize components
m_totalTf.setText(m_model.getValue());
m_totalTf.setEditable(false);
//... Structure the elements.
JPanel material = new JPanel();
material.setLayout(new FlowLayout());
material.add(new JLabel("Input"));
material.add(m_userInputTf);
material.add(m_multiplyBtn);
material.add(new JLabel("Total"));
material.add(m_totalTf);
material.add(m_clearBtn);
//... complete layout
this.setContentPane(content);
this.pack();
this.setTitle("Simple Calc - MVC");
// The screen ending occasion should probably be approved to the
// Operator in a actual system, but this is a brief example.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
gap reset() {
m_totalTf.setText(INITIAL_VALUE);
}
Sequence getUserInput() {
come back m_userInputTf.getText();
}
gap setTotal(String newTotal) {
m_totalTf.setText(newTotal);
}
gap showError(String errMessage) {
JOptionPane.showMessageDialog(this, errMessage);
}
gap addMultiplyListener(ActionListener mal) {
m_multiplyBtn.addActionListener(mal);
}
gap addClearListener(ActionListener cal) {
m_clearBtn.addActionListener(cal);
}
}
|
The Controller
The controller procedure the customer demands. It is applied here as an Viewer design -- the Operator signs up audience that are known as when the Perspective finds a customer connections. In accordance with the customer demand, the Operator contacting techniques in the Perspective and Style to achieve the asked for activity.
| // stucture/calc-mvc/CalcController.java - Controller
// Manages customer connections with audience.
// Calls Perspective and Style as needed.
// Sam Swartz -- Dec 2004
import java.awt.event.*;
public category CalcController {
//... The Operator needs to communicate with both the Style and Perspective.
personal CalcModel m_model;
personal CalcView m_view;
//========================================================== constructor
/** Constructor */
CalcController(CalcModel model, CalcView view) {
m_model = model;
m_view = view;
//... Add audience to the scene.
view.addMultiplyListener(new MultiplyListener());
view.addClearListener(new ClearListener());
}
////////////////////////////////////////// inner category MultiplyListener
/** When a mulitplication is asked for.
* 1. Get the customer feedback variety from the Perspective.
* 2. Contact the model to mulitply by this variety.
* 3. Get the outcome from the Style.
* 4. Tell the Perspective to show the outcome.
* If there was a mistake, tell the Perspective to show it.
*/
category MultiplyListener utilizes ActionListener {
community gap actionPerformed(ActionEvent e) {
Sequence userInput = "";
try {
userInput = m_view.getUserInput();
m_model.multiplyBy(userInput);
m_view.setTotal(m_model.getValue());
} capture (NumberFormatException nfex) {
m_view.showError("Bad input: '" + userInput + "'");
}
}
}//end inner category MultiplyListener
//////////////////////////////////////////// inner category ClearListener
/** 1. Totally reset model.
* 2. Totally reset Perspective.
*/
category ClearListener utilizes ActionListener {
community gap actionPerformed(ActionEvent e) {
m_model.reset();
m_view.reset();
}
}// end inner category ClearListener
}
|
Model
The model is individual of the user interface. It doesn't know if it's being used from a text-based, visual, or web user interface. This is the same model used in the presentation example.
| // structure/calc-mvc/CalcModel.java
// Sam Swartz - Dec 2004
// Model
// This model is absolutely individual of the user interface.
// It could as quickly be used by a control range or web user interface.
import java.math.BigInteger;
public category CalcModel {
//... Constants
personal fixed last Sequence INITIAL_VALUE = "0";
//... Participant varying interpreting condition of finance calculator.
personal BigInteger m_total; // The complete present value condition.
//============================================================== constructor
/** Constructor */
CalcModel() {
reset();
}
//==================================================================== reset
/** Totally reset to preliminary value. */
community gap reset() {
m_total = new BigInteger(INITIAL_VALUE);
}
//=============================================================== multiplyBy
/** Increase present complete by a variety.
*@param operand Number (as string) to multiply complete by.
*/
community gap multiplyBy(String operand) {
m_total = m_total.multiply(new BigInteger(operand));
}
//================================================================= setValue
/** Set the complete value.
*@param value New value that should be used for the finance calculator complete.
*/
community gap setValue(String value) {
m_total = new BigInteger(value);
}
//================================================================= getValue
/** Return present finance calculator complete. */
community Sequence getValue() {
come back m_total.toString();
}
}
|







No comments:
Post a Comment