`
yanfaguanli
  • 浏览: 663176 次
文章分类
社区版块
存档分类
最新评论

04(设计)什么是MVC模式

 
阅读更多

好吧我知道,应该很少人一开始学网站开发就从MVC开始,但如果你已经理解了三层架构之类的,那直接尝试强大的微软MVC网站开发模式也是挺不错的。

但其实我们学校有个实验室,那些干进去的就算是大一的学生,也是直接开始使用鲁比语言(我忘了英文怎么拼了~)的MVC模式开发网站,而且是可以真实部署到客户厂家进行使用的。

下面开始介绍MVC模式。

MVC,即Model模型、View视图、Controller控制器

用户发送请求到ControllerController将请求规范化交给Model处理,Model调用(可以通过BLLDAL)数据库信息,得到数据后以“表”的形式返回给ControllerController将表信息交给View处理,View将其“美化”展示给用户。

其实要用文字来表达什么是MVC可能理解起来是有的麻烦的,比较得自己多找点资料吧。

这里我再贴出JAVA课写出来的JAVA MVC 代码,不拖一个空间就可以实现该效果:


文件有:


代码如下:

Model

package mvccalculator;

// The Model performs all the calculations needed
// and that is it. It doesn't know the View 
// exists

public class CalculatorModel {

	// Holds the value of the sum of the numbers
	// entered in the view
	
	private int calculationValue;
	
	public void addTwoNumbers(int firstNumber, int secondNumber){
		
		calculationValue = firstNumber + secondNumber;
		
	}
        
        public void subTwoNumbers(int firstNumber, int secondNumber){
		
		calculationValue = firstNumber - secondNumber;
		
	}
        
        public void mulTwoNumbers(int firstNumber, int secondNumber){
		
		calculationValue = firstNumber * secondNumber;
		
	}
        
        public void divTwoNumbers(int firstNumber, int secondNumber){
		
		calculationValue = firstNumber / secondNumber;
		
	}
	
	public int getCalculationValue(){
		
		return calculationValue;
		
	}
	
}

View

package mvccalculator;

// This is the View
// Its only job is to display what the user sees
// It performs no calculations, but instead passes
// information entered by the user to whomever needs
// it. 

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

public class CalculatorView extends JFrame{

	private ArrayList<JTextField> firstNumber  = new ArrayList<JTextField>();
	private ArrayList<JLabel> additionLabel = new ArrayList<JLabel>();
	private ArrayList<JTextField> secondNumber = new ArrayList<JTextField>();
	private ArrayList<JButton> calculateButton = new ArrayList<JButton>();
	private ArrayList<JTextField> calcSolution = new ArrayList<JTextField>();
	
	CalculatorView(){
		
		// Sets up the view and adds the components
		
		ArrayList<JPanel> calcPanel = new ArrayList<JPanel>();
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(600, 200);
                this.setLayout(new GridLayout(4,1,0,0));
		String[] str = {"+", "-", "*", "/"};
                for(int i = 0;i < 4;i++)
                {
                    firstNumber.add(new JTextField(10));
                    additionLabel.add(new JLabel(str[i]));
                    secondNumber.add(new JTextField(10));
                    calculateButton.add(new JButton("Calculate"));
                    calcSolution.add(new JTextField(10));
                    
                    calcPanel.add(new JPanel());
                            
                    calcPanel.get(i).add(firstNumber.get(i));
                    calcPanel.get(i).add(additionLabel.get(i));
                    calcPanel.get(i).add(secondNumber.get(i));
                    calcPanel.get(i).add(calculateButton.get(i));
                    calcPanel.get(i).add(calcSolution.get(i));
                    
                    this.add(calcPanel.get(i),i);
                }
		
		
		
		
		// End of setting up the components --------
		
	}
	
	public int getFirstNumber(int i){
		
		return Integer.parseInt(firstNumber.get(i).getText());
		
	}
	
	public int getSecondNumber(int i){
		
		return Integer.parseInt(secondNumber.get(i).getText());
		
	}
	
	public int getCalcSolution(int i){
		
		return Integer.parseInt(calcSolution.get(i).getText());
		
	}
	
	public void setCalcSolution(int i, int solution){
		
		calcSolution.get(i).setText(Integer.toString(solution));
		
	}
	
	// If the calculateButton is clicked execute a method
	// in the Controller named actionPerformed
	
	void addCalculateListener(int i, ActionListener listenForCalcButton){
		
		calculateButton.get(i).addActionListener(listenForCalcButton);
		
	}
	
	// Open a popup that contains the error message passed
	
	void displayErrorMessage(String errorMessage){
		
		JOptionPane.showMessageDialog(this, errorMessage);
		
	}
	
}

Control

package mvccalculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// The Controller coordinates interactions
// between the View and Model

public class CalculatorController {
	
	private CalculatorView theView;
	private CalculatorModel theModel;
	
	public CalculatorController(CalculatorView theView, CalculatorModel theModel) {
		this.theView = theView;
		this.theModel = theModel;
		
		// Tell the View that when ever the calculate button
		// is clicked to execute the actionPerformed method
		// in the CalculateListener inner class
		for(int i = 0;i < 4;i++)
                {
                    this.theView.addCalculateListener(i, new CalculateListener(i));
                }
	}
	
	class CalculateListener implements ActionListener{
            int index = -1;
        private CalculateListener(int i) {
            index = i;
        }

		public void actionPerformed(ActionEvent e) {
			
			int firstNumber = 0;
                        int secondNumber = 0;
			
			// Surround interactions with the view with
			// a try block in case numbers weren't
			// properly entered
			
			try{

                                
                                firstNumber = theView.getFirstNumber(index);
                                secondNumber = theView.getSecondNumber(index);
                                if(index == 0)
                                {
                                    theModel.addTwoNumbers(firstNumber, secondNumber);
                                }
                                else if(index == 1)
                                {
                                    theModel.subTwoNumbers(firstNumber, secondNumber);
                                }
                                else if(index == 2)
                                {
                                    theModel.mulTwoNumbers(firstNumber, secondNumber);
                                }
                                else if(index == 3)
                                {
                                    theModel.divTwoNumbers(firstNumber, secondNumber);
                                }

                                theView.setCalcSolution(index, theModel.getCalculationValue());


			}

			catch(NumberFormatException ex){
				
				System.out.println(ex);
				
				theView.displayErrorMessage("You Need to Enter 2 Integers");
				
			}
			
		}
		
	}
	
}

主类

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mvccalculator;

/**
 *
 * @author Administrator
 */
public class MVCCalculator {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    	
    	CalculatorView theView = new CalculatorView();
        
    	CalculatorModel theModel = new CalculatorModel();
        
        CalculatorController theController = new CalculatorController(theView,theModel);
        
        theView.setVisible(true);
        
    }
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics