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();
}
});
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。