MVC風格

       MVC風格

點擊瞭解更多軟件體系結構風格

§模型-視圖-控制器風格常被簡稱爲MVC風格

§組件:模型、視圖、控制器

§連接件:顯式調用、隱式調用、其他機制(例如:Http協議)

工作機制:

Model:

§模型的職責

–負責數據存取

–負責業務邏輯實現

–負責數據驗證

§模型:模型是應用程序的核心。它封裝內核數據與狀態,對模型的修改將擴散到所有視圖中。所有需要從模型獲取信息的對象都必須註冊爲模型的視圖。

§在事件驅動系統,當信息發生改變,模型通知觀察者(observers) (通常是視圖),從而引起視圖反應。

View:

§視圖的職責:

–獲取用戶輸入

–向controller發送處理請求

–接收來自Controller的反饋

–將model的處理結果顯示給用戶

§一個model可能有多個View

Controller:

§控制器職責:

–接收來自客戶的請求

–調用model執行

–調用View顯示執行結果

§控制器:控制器是提供給用戶進行操作的接口。每個視圖與一個控制器組件相關聯。控制器接收用戶的輸入,通常是鼠標移動、鍵盤輸入等。輸入事件翻譯成服務請求,送到模型或視圖。用戶只通過控制器與系統交互。

§兩個主要分離:

–模型與界面分離

–控制器與視圖分離

§優點:

–將各方面問題分解開來考慮,簡化了系統設計,保證了系統的可擴展性。

–改變界面不影響應用程序的功能內核,使得系統易於演化開發,可維護性好。

–同一信息可以有不同的展現方式。

–業務邏輯更易測試

程序:

模型類CarModel封裝二手車軟件的業務邏輯部分,包括核心數據以及tell()方法(將狀態的改變通知視圖類)

import java.io.*;

import java.net.URL;

import java.net.URI;

import javax.swing.*;

importjava.util.*;

 

public classCarModel{

    private String[] carNameList;

    privateURL imgURL;

    privateURL carFileUrl;

    privateImageIcon imgIcon;

       private String carSelected;

    private String bitPrice;

    static final String CARFILES ="CarFiles/";

    static final String CARIMAGES ="CarImages/";

 

    public CarModel(){

        carNameList=new String[200];

      }

    public void setCarList(String[] cars){

               carNameList = cars;

       }

    public String[] getCarList(){

             return  carNameList;

    }

    public void setSelectedCar(String sCar){

        carSelected = sCar;

      }

      publicString getSelectedCar(){

           return carSelected;

      }

      publicvoid setBitPrice(String bPrice){

              bitPrice = "";

              bitPrice = bitPrice + bPrice;

       }

       public String getBitPrice(){

                     return bitPrice;

       }

       public void setupImageIcon(){

                 String iconStr = CARIMAGES + carSelected+".jpg";

              imgIcon = createImageIcon(iconStr);

              }

    public ImageIcon getImageIcon(){

                 return imgIcon;

       }

       public void setCarFileURL(){

          try{

                 String fileURLStr = CARFILES + carSelected+ ".html";

                 URI uri = (new File(fileURLStr)).toURI();

                 carFileUrl= uri.toURL();

              }

              catch (IOException e){

                 e.printStackTrace();

              }

       }

       public URL getCarFileURL(){

              return carFileUrl;

       }

 

    protected ImageIcon createImageIcon(Stringpath){

       imgURL = getClass().getResource(path);

       if (imgURL != null) {

           return new ImageIcon(imgURL);

       } else {

           System.err.println("Couldn'tfind file: " + path);

           return null;

       }

   }

   public void tell(View view){

          view.update();

   }

} // End ofclass

 

視圖類CararGUIView和CarBitView爲圖形界面類,在update()方法中自動調用CarModel類的有關信息,並顯示在圖形界面上。

public interface View{

   publicabstract void update();

}

 

import java.awt.*;

import java.util.*;

import javax.swing.*;

import java.io.*;

import java.net.URL;

import java.awt.event.*;

import com.sun.java.swing.plaf.windows.*;

public classCarGUIView extends JFrame implements View{

   private JEditorPane editorPane;

   private JScrollPane imagePane;

   private JScrollPane textPane;

   private JSplitPane splitPane;

   private JLabel imgLabel;

   private CarModel model;

 

   public CarGUIView(CarModel cmodel){

      super("Car information- Observer1");

        model = cmodel;

      buildUpScrollGUI();

   }

   private void buildUpScrollGUI(){

      imgLabel = new JLabel();

      imgLabel.setBackground(Color.green);

        imgLabel.setMinimumSize(new Dimension(250, 200));

 

      editorPane = new JEditorPane();

        editorPane.setEditable(false);

 

      imagePane = new JScrollPane(imgLabel);

         imagePane.getViewport().setBackground(Color.green);

 

        textPane = new JScrollPane(editorPane);

        textPane.setMinimumSize(new Dimension(250, 200));

 

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        splitPane.setLeftComponent(imagePane);

         splitPane.setRightComponent(textPane);

 

        Dimension minimumSize = new Dimension(130, 100);

        imagePane.setMinimumSize(minimumSize);

        textPane.setMinimumSize(new Dimension(100, 100));

        splitPane.setDividerLocation(160);

        splitPane.setPreferredSize(new Dimension(500, 300));

 

        Container contentPane = getContentPane();

        contentPane.add(splitPane);

        setSize(400, 150);

        setVisible(true);

   }

 

    public void update(){

       try{

            URL url = model.getCarFileURL();

          editorPane.setPage(url);

          System.out.println("We have beencalled.");

         }

         catch (IOException e){

            e.printStackTrace();

       }

      ImageIcon imIcon = model.getImageIcon();

      imgLabel.setIcon(imIcon);

          imgLabel.validate();

    }

}

 

import java.awt.*;

importjavax.swing.*;

public classCarBitView extends JFrame implements View{

    private JPanel showPanel;

    private JLabel bitOfferedLabel;

    private JTextArea bitText;

    private CarModel model;

 

    //public CarBitView(CarModel cmodel) throwsException{

       public CarBitView(CarModel cmodel) {

       super("Car Bit Info View- Observer2");

       model = cmodel;

 

       bitOfferedLabel = newJLabel("Latest bit offered:");

       bitText = new JTextArea(4, 20);

       bitText.setFont(newFont("Serif", Font.PLAIN, 14));

       bitText.setLineWrap(true);

       bitText.setWrapStyleWord(true);

 

       Container contentPane =getContentPane();

       contentPane.add(bitOfferedLabel,BorderLayout.NORTH);

       contentPane.add(bitText,BorderLayout.CENTER);

 

       setSize(400, 150);

       setVisible(true);

     }

 

        public void update(){

       System.out.println("Car bit hasbeen called.");

       String sCar= model.getSelectedCar();

       String pr = model.getBitPrice();

       bitText.append("\n Bit price for"+ sCar + "="+ pr);

     }

}// end ofclass

 

控制器Controller負責根據CarAuctionGUI對象輸入的客戶選擇信息更新CarModel的數據。

importjava.awt.event.*;

importjavax.swing.*;

importjava.net.URL;

classController implements ActionListener{

   private CarAuctionGUI objCarGui;

   private CarModel cm;

   private CarGUIView civ;

   private CarBitView cb;

   private String carPrice;

   private String[] carList;

 

   public Controller(CarAuctionGUIobjCarGui,CarModel cm,

                     CarGUIView civ,CarBitViewcb){

      this.objCarGui = objCarGui;

      this.cm=cm;

      this.civ=civ;

      this.cb=cb;

 

        carList = objCarGui.getCarList();

        cm.setCarList(carList);

   }

   public void actionPerformed(ActionEvent e){

      String searchResult = null;

 

      if(e.getActionCommand().equals(CarAuctionGUI.EXIT)){

         System.exit(1);

      }

      if(e.getActionCommand().equals(CarAuctionGUI.SEARCH)){

         String selectedCar =objCarGui.getSelectedCar();

         cm.setSelectedCar(selectedCar);

         cm.setCarFileURL();

         cm.setupImageIcon();

         cm.tell(civ);

      }

      if(e.getActionCommand().equals(CarAuctionGUI.BIT)){

           carPrice = objCarGui.getBitPrice();

               cm.setBitPrice(carPrice);

         cm.tell(cb);

         }

   }

} // End ofclass Controller

 

importjava.awt.*;

import java.util.*;

import javax.swing.*;

import java.io.*;

import java.net.URL;

import java.awt.event.*;

import com.sun.java.swing.plaf.windows.*;

public classCarAuctionGUI extends JPanel {

   private JTextField bitInputText;

   private JLabel lblCarModel;

   private JPanel buttonPanel;

   private String[] carList;

   private JComboBox cmbCarList;

   private static CarModel cm;

   private static CarGUIView civ;

   private static CarBitView cb;

   public static final String SEARCH ="Search";

   public static final String BIT ="Bit";

   public static final String EXIT ="Exit";

 

   public CarAuctionGUI(){

        super(new GridLayout(1,0));

         setUpGUI();

   }

   private void setUpGUI(){

      cmbCarList = new JComboBox();

      String[] cl = getCarList();

     setUpCarList(cl);

      lblCarModel = new JLabel("Cars on auction:");

 

      //Create the open button

      JButton srchButton = new JButton(SEARCH);

      srchButton.setMnemonic(KeyEvent.VK_S);

      JButton exitButton = new JButton(EXIT);

      exitButton.setMnemonic(KeyEvent.VK_X);

      JButton bitButton = new JButton(BIT);

      bitButton.setMnemonic(KeyEvent.VK_X);

      bitInputText = new JTextField("Offeryour bit price",12);

      buttonPanel = new JPanel();

 

     //****************************************************

      GridBagLayout gridbag = newGridBagLayout();

      buttonPanel.setLayout(gridbag);

      GridBagConstraints gbc = new GridBagConstraints();

 

      buttonPanel.add(lblCarModel);

      buttonPanel.add(cmbCarList);

      buttonPanel.add(srchButton);

      buttonPanel.add(bitButton);

      buttonPanel.add(exitButton);

      buttonPanel.add(bitInputText);

 

      gbc.insets.top = 5;

      gbc.insets.bottom = 5;

      gbc.insets.left = 5;

      gbc.insets.right = 5;

      gbc.anchor = GridBagConstraints.EAST;

      gbc.gridx = 0;

      gbc.gridy = 0;

      gridbag.setConstraints(lblCarModel, gbc);

      gbc.anchor = GridBagConstraints.WEST;

      gbc.gridx = 1;

      gbc.gridy = 0;

      gridbag.setConstraints(cmbCarList, gbc);

      gbc.anchor = GridBagConstraints.EAST;

      gbc.insets.left = 2;

      gbc.insets.right = 2;

      gbc.insets.top = 25;

      gbc.anchor = GridBagConstraints.EAST;

      gbc.gridx = 0;

      gbc.gridy = 3;

      gridbag.setConstraints(srchButton, gbc);

      gbc.anchor = GridBagConstraints.WEST;

      gbc.gridx = 1;

      gbc.gridy = 3;

      gridbag.setConstraints(exitButton, gbc);

      gbc.gridx = 0;

        gbc.gridy = 4;

      gridbag.setConstraints(bitButton, gbc);

      gbc.gridx = 1;

        gbc.gridy = 4;

      gridbag.setConstraints(bitInputText,gbc);

      Controller objButtonHandler = newController(this,cm,civ,cb);

      srchButton.addActionListener(objButtonHandler);

     exitButton.addActionListener(objButtonHandler);

     bitButton.addActionListener(objButtonHandler);

 

      add(buttonPanel);

        setSize(new Dimension(800, 450));

      setVisible(true);

    }

 

    public String getSelectedCar() {

          return (String) cmbCarList.getSelectedItem();

       }

 

       public String getBitPrice(){

         return       bitInputText.getText();

       }

 

       // get the names of all the .html filesin a directory

       public String[] getCarList(){

         File f = new File("CarFiles");

         String [] fileNames = f.list();

 

         for(int i=0; i<fileNames.length; i++ ){

            int len = fileNames[i].length();

               fileNames[i]=fileNames[i].substring(0,len-5);

         }

         return fileNames;

   }

 

       public void setUpCarList(String[]carList){

         for(int k=0; k<carList.length; k++) {

            cmbCarList.addItem(carList[k]);

         }

       }

 

  private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);

     JFrame frame = new JFrame("MVCpattern demo");

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

     CarAuctionGUI newContentPane = newCarAuctionGUI();

     newContentPane.setOpaque(true);

     frame.setContentPane(newContentPane);

 

     //Display the window.

     frame.pack();

     frame.setVisible(true);

  }

 

  static public void main(String argv[]) {

        javax.swing.SwingUtilities.invokeLater(newRunnable() {

     public void run() {

          cm = new CarModel();

          civ= new CarGUIView(cm);

          cb = new CarBitView(cm);

        createAndShowGUI();

        }

        });

  }

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章